two lists and returns the longer list #!/usr/bin/perl use strict; use warnings; sub longer { my (@l1, @l2) = @_; return @l1 > @l2 ? @l1 : @l2; } Friday, November 4, 2011
# create a list ref with \@ my $l_ref = \@l; # create a hash ref with \% my $h_ref = \%h; # create a scalar ref with \$ my $x_ref = \$x; # create a subroutine ref with \& my $f_ref = \&foo; Friday, November 4, 2011
Operation is called dereference # note that assignment operator # generates a copy operation my @copy = @$l_ref; my %copy = %$h_ref; my $copy = $$x_ref; my $res = &$f_ref; Friday, November 4, 2011
operator Note the different type of parens # Direct element access using # references my $first = $l_ref->[0]; my $val = $h_ref->{key}; my $result = $f_ref->('x', 'y'); Friday, November 4, 2011
and return a ref Use { ... } to create a hash and return a ref my $al_ref = [2, 3, 5]; my $ah_ref = { Adam => 'Eve', Bonnie => 'Clyde', }; Friday, November 4, 2011
a reference, we call that anonymous reference As long as some ref to it is still in scope, the entity won’t be deleted sub gen_colors { [qw/red blue green/] } my $colors = gen_colors; print $colors->[1], "\n"; Friday, November 4, 2011
reference at hand Can be helpful when implementing subroutine argument checks use strict; use warnings; my $coderef = sub { print "Hello World\n"; }; my $lref = [1,2,3,4]; my $href = { a => 1, b => 2 }; $coderef->(); print 'coderef is of type: ', ref $coderef; print 'lref is of type: ', ref $lref; print 'href is of type: ', ref $href; Friday, November 4, 2011
4, 3], [qw/a b c/]]; # prints 4 print $nested_ref->[2]->[1], "\n"; # prints b print $nested_ref->[3]->[1], "\n"; # prints 4 print scalar @$nested_ref; Friday, November 4, 2011
sub from the array my $actions = [ sub { print "hello\n" }, sub { print 2 + 2 }, sub { die "Muhahaha" }, ]; $actions->[int rand(@$actions)]->(); Friday, November 4, 2011
can now keep an array of subs, or hash of subs We can now use subs that take other subs as input We can even write subs that return other subs Friday, November 4, 2011
a name returns a reference Note the ; at the end of the assignment my $f_ref = sub { my ($x, $y) = @_; return $x + $y; }; # returns 5 $f_ref->(2, 3); Friday, November 4, 2011
send to your subroutine, consider wrapping it in a hashref Best Practice: Send at most 3 arguments to subs sub print_data_bad { my ($name, $color, $age, $home, $work) = @_; } sub print_data_good { my ($name, $params) = @_; my $color = $params->{color} || "blue"; my $age = $params->{age} || 0; my $home = $params->{home}; my $work = $params->{work}; } print_data_good('James', { age => 18, home => 'Moon' }); Friday, November 4, 2011
type croak on invalid argument use Carp; sub print_data_good { my ($name, $params) = @_; croak "invalid argument" if ref($params) ne 'HASH'; my $color = $params->{color} || "blue"; my $age = $params->{age} || 0; my $home = $params->{home}; my $work = $params->{work}; } print_data_good('James', { age => 18, home => 'Moon' }); Friday, November 4, 2011
real programming language References allow complex data to be processed with ease using perl References form the base for Object Oriented Perl Friday, November 4, 2011