Arguments in @_
Unpack @_
Every subroutine receives its arguments as one flat list in @_. Name them on the first line with a list assignment, which also documents the signature for the reader.
sub area { my ($width, $height) = @_; return $width * $height; } print area(3, 4), "\n"; # 12
perl demo.pl
12
Extra arguments are simply ignored, and missing ones arrive as undef. Perl does not check the count, so the subroutine has to. A guard at the top is worth more than a comment:
sub area { my ($width, $height) = @_; die "area needs two numbers\n" unless defined $height; return $width * $height; } print area(3, 4), "\n"; # 12 print area(3), "\n"; # area needs two numbers
perl demo.pl
12
area needs two numbers
Defaults
A default value is a defined-or assignment after unpacking. Use //= rather than ||=, so a caller who passes 0 or an empty string keeps it.
sub greet { my ($name) = @_; $name //= "world"; return "hello, $name"; } print greet(), "\n"; # hello, world print greet(""), "\n"; # hello,
perl demo.pl
hello, world
hello,
It is an alias
The elements of @_ are aliases to the caller values. Changing $_[0] changes the variable that was passed, which is how some old functions return several results.
sub bump { $_[0]++; } my $n = 1; bump($n); print "$n\n"; # 2
perl demo.pl
2
Copying into my variables, as every example above does, breaks the alias and protects the caller. Do that by default, and use the alias deliberately when you really want to modify in place.
Named arguments
Once a subroutine takes more than three arguments, positional order stops being memorable. Pass a flat list of key-value pairs instead and unpack it into a hash.
sub db_connect { my %opt = @_; return "$opt{host}:$opt{port}"; } print db_connect(host => "db1", port => 5432), "\n";
perl demo.pl
db1:5432
The fat comma makes the call read like a table. Watch out for a hash argument passed unwrapped: a hash flattens into pairs, so wrap it in braces to pass a reference instead.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a subroutine receive in
@_?Answer
One flat list of all the arguments
Every call hands over a single list, and
my ($a, $b) = @_;unpacks and documents it. -
What happens when a caller passes fewer arguments than the subroutine names?
Answer
The missing ones are undef and the subroutine runs anyway, so it has to check
Perl does not check the count, which is why a
die ... unless defined $heightguard belongs at the top. -
Why use
//=rather than||=for a default argument?Answer
//=only replaces undef, so a caller who passes 0 or an empty string keeps that value||=treats 0 and "" as missing, which silently overrides a caller who deliberately passed them. -
What do the elements of
@_alias?Answer
The variables the caller passed, so changing
$_[0]changes the caller valueThe alias is why
sub bump { $_[0]++ }can modify the caller, and why copying intomyprotects it. -
Which line breaks the alias to the caller values?
Answer
my ($x) = @_;The list assignment copies the values into new lexicals, so later changes stay inside the subroutine.
-
Why does a subroutine that takes a hash need
db_connect({ %opt })instead ofdb_connect(%opt)when one argument must stay a hash?Answer
A hash flattens into key-value pairs in the argument list, so braces pass a reference instead
The flat list cannot hold a nested hash, so the reference in braces keeps it one value.
-
What does this program print?
sub area { my ($w, $h) = @_; return $w * $h; } print area(3, 4), "\n";Answer
12
The two arguments are unpacked into named variables and multiplied.
-
What does this program print?
sub bump { $_[0]++ } my $n = 1; bump($n); print "$n\n";Answer
2
The increment lands on the caller variable through the alias in
@_. -
What does this program print?
sub port { my ($p) = @_; $p //= 80; return $p; } print port(0), "\n";Answer
0
The caller passed a defined 0, so the defined-or assignment leaves it alone.
-
What does this program print?
sub db_connect { my %opt = @_; return "$opt{host}:$opt{port}"; } print db_connect(host => "db1", port => 5432), "\n";Answer
db1:5432
The flat pair list fills the hash, and the string interpolation reads both keys.
Article author: Arthur Isaev