September 2017
Beginner
402 pages
9h 52m
English
In Perl 5, you had to extract the values of the arguments of a function yourself by using either the built-in shift function, or from the default @_ array.
Let's see this in the following example, with a function to calculate the sum of its two arguments. In Perl 5, you had to do some additional work to get the actual passed parameters.
First, get the argument values with shift in Perl 5:
sub add { my $x = shift; my $y = shift; return $x + $y;}
Then, by using the @_ array:
sub add { my ($x, $y) = @_; return $x + $y;}
Unlike many other programming languages, it was not possible to declare a list of the function's formal parameters directly. For instance, this is how you do it in C or C++:
int add(int x, int y) {
return x + y; ...