The understanding of passing references is REALLY important for OO-Perl. I have spent much time playing with all the possible syntaxes of referencing and de-referencing. I'm sure there are always more things to learn about the subject. But I suggest you all discover all you can.
Play with referenced multi-depth structures as much as possible. For example:
#!/usr/bin/perl -w
use strict;
sub FullStru { # creates the structures
my $stru = { # an annonymous hash (ad hoc hash)
Level2a => { # another ad hoc hash
Level3a => ['a'..'z'], # an ad hoc list at
+level 3
Level3b => [1..10] # another
},
Level2b => [ qw(word1 word2 word3) ] # an ad hoc list
+at level 2
};
return $stru; # notice that this is a reference to an annoymous ha
+sh with sub levels
}
sub DownOne {
my $ref = shift; # by using a reference, you get the whole enchila
+da
if ( ref($ref) eq 'HASH') {
# return the elements as list. No real practical value other
# than as an exercise.. at least as much as I can see
return 'ref to HASH', values %$ref;
} elsif (ref($ref) eq 'ARRAY') {
# return the elements as a list
return 'ref to ARRAY', @$ref;
} elsif (ref($ref) eq 'SCALAR') {
return 'ref to SCALAR', $$ref;
} elsif ( not ref($ref)) {
return 'not a ref';
} # and so on..
}
my $X = FullStru(); # create the structure and return a pointer to it
# first level
my @results1 = DownOne($X);
print join("\n",@results1),"\n";
shift(@results1); # get rid of that first element
# second level
my $elem;
foreach $elem (@results1) {
print "\n\t",join("\n\t",DownOne($elem)),"\n";
}
# and so on.. you get the idea
The result should look like this:
ref to HASH
HASH(0x1d5f518)
ARRAY(0x1d5f548)
ref to HASH
ARRAY(0x22526c)
ARRAY(0x1d5f488)
ref to ARRAY
word1
word2
word3