7.1 Defining Subroutines
Defining a subroutine is quite easy. You use the keyword sub
,
followed by the name of your subroutine, followed by a code block. This
friendly subroutine can be used to greet the user:
use strict;
sub HowdyEveryone {
print "Hello everyone.\nWhere do you want to go with Perl today?\n";
}
Now, anywhere in the code where we want to greet the user, we can simply
say:
&HowdyEveryone;
and it will print that message to the user. In fact, in most cases, the
&
for invoking subroutines is optional.