Subroutines
Suppose that you want to write a subroutine to compute the difference between two time values. Start your subroutine definition like this:
sub timediff($$) {
The sub keyword signals the start of a subroutine. In this case, the subroutine is named timediff. The function takes two scalar parameters. This is indicated by the parameter list ($$).
The method Perl uses to pass parameters is unique: passing by special variable. When a function is called, Perl assigns the parameters to the array @_. You now need to get the parameters out of the @_ array and into something you can use.
One simple way of doing this is to use the shift function to “shift” the parameters out of @_ and into simple variables:
my $start_time = shift; # Time the ...
Get Perl for C Programmers 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.