6. References

Created Friday 18 April 2014

Array References

my @fools = qw(jester clown motley);
my $fools = \@fools;
my @copy_of_fools = @$fools;
my @fools = qw(jester clown motley);
my $aref = \@fools;
my $first_fool = $aref->[0];
my $last_fool = $aref->[2];
print "$first_fool - $last_fool";
foreach my $fool (@$aref) {
print "$fool\n";
}
my @fools = qw(jester clown motley);
my $fools = \@fools;
foreach my $i (0 .. $#$fools) {
my $fool = $fools->[$i];
print "$fool\n";
}
foreach my $i (0 .. $#$fools) {
print "$fools->[$i]\n";
}

Hash References

my %words = (
dog => 'chien',
eat => 'manger',
clown => 'clown',
);
my $english_to_franch = \%words;
my %copy = %$english_to_french;
my $eat = $english_to_french->{eat};
while (my ($english, $french) = each %$english_to_french) {
print "The french word for '$english' is '$french'\n";
}

Ways to acces elements in a reference

Anonymous References

my $stuff = ['foo', 'bar', 'baz'];
my $colors = {red => '#FF0000', green => '#00FF00', blue => '#0000FF'};

Anonymous Arrays

An array of arrays (AoA):
my @results = (

[12, 19, 4 ],
[454, 2, 42 ],
[6, 9, 13, 44],
);
my $aref1 = $results[0];
my $aref2 = $results[1];
my $aref3 = $results[2];
my $number = $aref2->[2];
my $number = $results[1]->[2];
my $number = $aoaoa[3]->[1]->[0];
my $number = $aoaoa[3][1][0];
push @$array, $value;
push @{$some_array[3][0]}, $some_value;

Anonymous Hashes

my $sales = {
monday => {jim => 2, mary => 1},
tuesday => {jim => 3, mary => 5},
wednesday => {jim => 7, mary => 3},
thursday => {jim => 4, mary => 5},
friday => {jim => 1, mary => 2},
};
my $num_sales = $sales->{friday}{mary};
my $num_sales = $sales->{friday}->{mary};
my @days_of_the_week = keys %$sales;
my @sales_people = keys %{$sales->{monday}};

Other References

my $add_two = sub {
my $number = shift;
return $number + 2;
};
print $add_two->(7);
my $name = 'Ovid';
my $ref = \$name;
print $$ref;

WORKING WITH REFERENCES

Debugging

A handy little program that shows various reference types:
use strict;
use warnings;
use CGI;

my $foo;
sub handler {};

my $scalar = ref $foo;
my $scalarref = ref \$foo;
my $arrayref = ref \@ARGV;
my $hashref = ref \%ENV;
my $coderef = ref \&handler;
my $globref = ref \*foo;
my $regexref = ref gr//;
my $objectref = ref CGI->new;

print <<"END_REFERENCES";
Scalar: $scalar
Scalar ref : $scalarref
Array ref: $arrayref
Hash ref: $hashref
Code ref: $coderef
Glob ref: $globref
Regex ref: $regexref
Object ref: $objectref
END_REFERENCES

use Data::Dumper;
print Dumper($aref);
print Dumper(\@words);

Copying

use Data::Dumper;
my $aref1 = [1, 3, 7];
my $aref2 = $aref1;
$aref2->[0] = 9;
print Dumper($aref1, $aref2);

use Data::Dumper;
my $aref1 = [1, 3, 7];
my $aref2 = [@$aref1];
$aref2->[0] = 9;
print Dumper($aref1, $aref2);
use strict;
use warnings;
use diagnostics;
use Data::Dumper;
use Storable 'dclone';

my %old_sales = (

monday => {jim => 2, mary => 1},
tuesday => {jim => 3, mary => 5},
wednesday => {jim => 7, mary => 3},
thursday => {jum => 4, mary => 5},
friday => {jim => 1, mary => 2},
);

my %new_sales = %{dclone(\%old_sales)};
while (my ($day, $sales) = each %new_sales) {

$sales->{jim} = 0;
$sales->{mary} = 0;
}
print Dumper(\%old_sales, \%new_sales);

Slices

# array slice
my @array = qw(foo bar baz quux);
my ($var1, $var2) = @array[1, 2];
# hash slice
my %hash = (
this => 'is',
another => 'boring',
example => 'innit?'
);
my ($first, $second) = @hash{'another', 'example'};
print "$var1, $var2\n";
print "$first, $second\n";
# array slice
my $arrayref = [qw(foo bar baz quux)];
my ($var1, $var2) = @$arrayref[1, 2];
#hash slice
my $hashref = {
this => 'is',
another => 'boring',
example => 'innit'
};
my ($first, $second) = @$hashref{'another', 'example'};
print "$var1, $var2\n";
print "$first, $second\n";
my ($jim, $mary, $alice)
= @{$sales->[12]{tuesday}}{qw/ jim mary alice /};



Backlinks: