Arguments in @_

Contents
Unpack @_
Defaults
It is an alias
Named arguments

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.

Next: References

Article author: Arthur Isaev

Related articles
Perl
Perl course: zero to hero in 50 lessons
What is Perl and where it runs
Install Perl and check the version
Your first Perl script
The debugger as a REPL
Scalars and sigils
Numbers
Strings
Interpolation and quoting
Arrays
push, pop, shift and splice
Hashes
keys, values, exists and delete
Scalar and list context
Conditions and comparison
while, for, next and last
foreach and the default variable
Subroutines
Arguments in @_
References
Nested data structures
Regular expressions: match and anchors
Captures and named groups
Substitution with s///
split and join
grep and map
Sort and the Schwartzian transform
Read and write files
File tests and metadata
Directories and paths
die, warn and eval
Modules and Exporter
CPAN and cpanm
Objects with bless
Modern objects with Moo
Testing with Test::More
strict, warnings and diagnostics
Options with Getopt::Long
Environment and %ENV
system, backticks and pipes
One-liners with -n and -p
CSV and TSV processing
JSON with JSON::PP
HTTP clients with HTTP::Tiny
Sockets and IO::Socket
Databases with DBI
Capstone: todo CLI
Capstone: log report
Performance and profiling
Modern Perl tour
Hero roadmap: the whole course on one page
Install Perl on Windows 11
Install Perl on Ubuntu
Install Perl on Rocky Linux
Install Perl on macOS
Install Perl on FreeBSD
Perl in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: