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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.