I wont reiterate the importance of 'strict' and 'warnings' ... (it is very useful) instead, I'm gonna tell you what you won't hear from anyone else.
I post this because you said this is your first programming language. Others will disagree with what I am about to tell you, and this may seem like weird advice, but you can judge for yourself.
... Disclaimer: extremely biased opinion follows ...
IMHO the single best way to 'wrap your brain' around working with references is to absolutely avoid using the 'backslash notation'
whenever possible, and just treat everything as a scalar. This may take a moment to sink in, but let me explain, and consider this example ...
### re-worked version of your original code
my $ref1 = [(0..9)]; ### start with anon array refs
my $ref2 = [('a'..'l')]; ### from the very beginning
my ($aOne, $aTwo) = &f($ref1, $ref2);
print '@a1 is: '. "@{$aOne}\n";
print '@a2 is: '. "@{$aTwo}\n";
sub f {
my $a1 = shift || die 'Hey! I expected an array ref ';
my $a2 = shift || die 'expected array ref here too ';
### do something, for example ...
@{$a2} = reverse @{$a2};
return ($a1, $a2);
}
__END__
@a1 is: 0 1 2 3 4 5 6 7 8 9
@a2 is: l k j i h g f e d c b a
rationale
- the little 'backslash notation' just sucks. It is ugly, it is error prone, it is confusion-inducing, and it is one of the worst widespread typographical conventions in the entire field of computer programming. Avoid using it with your variables. (aside from stuff like \n, which is basically a constant) Your brain will thank you for it.
- just think to yourself "scalars can hold *anything*", not just strings ... then you realize the hardest part is just picking a variable name (eg $ref1, $aryRef1, $hashRef1, $bigHugeNestedVariable1, etc) to remind you of what that scalar is expected to be holding
- suppose you want to change your arrays to hashes or something else? It's a PITA to go around changing all those little \@ symbols to \% symbols, and much clearer to change 'die expected array ref' to 'die expected hash ref'. Either way, you will have to change the code, but one set of changes is easier on the cognitive process than the other. You can decide for yourself which ones.
Just remember: 1) "scalars can hold anything" (the variables with the dollar sign in front) use them; 2) give those scalars a good name to reflect what's expected of them; and 3)properly 'morph' those scalars into proper versions of what they are holding with @{$foo}, @$foo, %{$foo}, or %$foo, notation .. or $foo->(), $foo->{baz}{blee}{blaa}( or whatever else is appropriate ) and you're done.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.