Invoking a Subroutine

Invoke a subroutine from within any expression by using the subroutine name (with the ampersand):[96]

    &marine;  # says Hello, sailor number 1!
    &marine;  # says Hello, sailor number 2!
    &marine;  # says Hello, sailor number 3!
    &marine;  # says Hello, sailor number 4!

Most often, we refer to the invocation as calling the subroutine.



[96] And frequently a pair of parentheses, even if empty. As written, the subroutine inherits the caller’s @_ value, which we’ll be discussing shortly. So don’t stop reading here, or you’ll be writing code with unintended effects.

Get Learning Perl, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.