Subroutines and Arguments
Default Arguments
A subroutine can fill in a missing argument with a default value.
Default Arguments
default_arguments.pl
use strict;
use warnings;
sub count_label {
my ($count, $noun) = @_;
$noun //= "item";
my $label = $count == 1 ? $noun : $noun . "s";
return "$count $label";
}
my $count = ;
my $text = count_label($count);
print "count=$count\n";
print "text=$text\n";
use strict;
use warnings;
sub count_label {
my ($count, $noun) = @_;
$noun //= "item";
my $label = $count == 1 ? $noun : $noun . "s";
return "$count $label";
}
my $count = ;
my $text = count_label($count);
print "count=$count\n";
print "text=$text\n";
use strict;
use warnings;
sub count_label {
my ($count, $noun) = @_;
$noun //= "item";
my $label = $count == 1 ? $noun : $noun . "s";
return "$count $label";
}
my $count = ;
my $text = count_label($count);
print "count=$count\n";
print "text=$text\n";
default argument
A default argument supplies a fallback when the caller omits a value.