- publishing free software manuals
Perl Language Reference Manual
by Larry Wall and others
Paperback (6"x9"), 724 pages
ISBN 9781906966027
RRP £29.95 ($39.95)

Sales of this book support The Perl Foundation! Get a printed copy>>>

2.11 Writing subroutines

Writing subroutines is easy:

sub logger {
    my $logmessage = shift;
    open my $logfile, ">>", "my.log" 
        or die "Could not open my.log: $!";
    print $logfile $logmessage;
}

Now we can use the subroutine just as any other built-in function:

logger("We have a logger subroutine!");

What's that shift? Well, the arguments to a subroutine are available to us as a special array called @_ (see 10 for more on that). The default argument to the shift function just happens to be @_. So my $logmessage = shift; shifts the first item off the list of arguments and assigns it to $logmessage.

We can manipulate @_ in other ways too:

my ($logmessage, $priority) = @_;       # common
my $logmessage = $_[0];                 # uncommon, and ugly

Subroutines can also return values:

sub square {
    my $num = shift;
    my $result = $num * $num;
    return $result;
}

Then use it like:

$sq = square(8);

For more information on writing subroutines, see 8.

ISBN 9781906966027Perl Language Reference ManualSee the print edition