References and Nested Data
Passing References
A subroutine can receive a reference and read the data it points to.
Passing References
pass_refs.pl
use strict;
use warnings;
sub total_points {
my ($values_ref, $bonus) = @_;
my $total = $values_ref->[0] + $values_ref->[1] + $values_ref->[2];
$total = $total + $bonus;
return $total;
}
my $bonus = ;
my $values_ref = [2, 4, 6];
my $total = total_points($values_ref, $bonus);
print "bonus=$bonus\n";
print "total=$total\n";
use strict;
use warnings;
sub total_points {
my ($values_ref, $bonus) = @_;
my $total = $values_ref->[0] + $values_ref->[1] + $values_ref->[2];
$total = $total + $bonus;
return $total;
}
my $bonus = ;
my $values_ref = [2, 4, 6];
my $total = total_points($values_ref, $bonus);
print "bonus=$bonus\n";
print "total=$total\n";
use strict;
use warnings;
sub total_points {
my ($values_ref, $bonus) = @_;
my $total = $values_ref->[0] + $values_ref->[1] + $values_ref->[2];
$total = $total + $bonus;
return $total;
}
my $bonus = ;
my $values_ref = [2, 4, 6];
my $total = total_points($values_ref, $bonus);
print "bonus=$bonus\n";
print "total=$total\n";
reference argument
A reference argument lets a subroutine work with a shared data structure.