Passing arguments to a subroutine

You can pass arguments to a subroutine by including them in the brackets when you call it. The arguments end up in an array called @_ which is only visible inside the subroutine.

print_headers("Programming Perl, 2nd ed", "Larry Wall et al");

# we can also pass variables to a subroutine by name...
my $fiction_title = "Lord of the Rings";
my $fiction_author = "J.R.R. Tolkein";
print_headers($fiction_title, $fiction_author);

sub print_headers {
        my ($title, $author) = @_;
        print "$title\n";
        print "by\n";
        print "$author\n";
}

You can take any number of scalars in as arguments - they'll all end up in @_ in the same order you gave them.

You could also use $title = shift; $author = shift; to get the same result. See the entry for shift on page 215 of the Camel book.