for $i (1..10) {
@list = somefunc($i);
$LoL[$i] = @list; # WRONG!
}
That's just the simple case of assigning a list to a scalar
and getting its element count. If that's what you really
and truly want, then you might do well to consider being a
tad more explicit about it, like this:
for $i (1..10) {
@list = somefunc($i);
$counts[$i] = scalar @list;
}
Here's the case of taking a reference to the same memory
location again and again:
for $i (1..10) {
@list = somefunc($i);
$LoL[$i] = \@list; # WRONG!
}
30/Jan/96 Last change: perl 3
PERLDSC(1) Perl Programmers Reference Guide PERLDSC(1)
So, just what's the big problem with that? It looks right,
doesn't it? After all, I just told you that you need an
array of references, so by golly, you've made me one!
Unfortunately, while this is true, it's still broken. All
the references in @LoL refer to the _v_e_r_y _s_a_m_e _p_l_a_c_e, and
they will therefore all hold whatever was last in @list!
It's similar to the problem demonstrated in the following C
program:
#include <pwd.h>
main() {
struct passwd *getpwnam(), *rp, *dp;
rp = getpwnam("root");
dp = getpwnam("daemon");
printf("daemon name is %s\nroot name is %s\n",
dp->pw_name, rp->pw_name);
}
Which will print
daemon name is daemon
root name is daemon
The problem is that both rp and dp are pointers to the same
location in memory! In C, you'd have to remember to
_m_a_l_l_o_c() yourself some new memory. In Perl, you'll want to
use the array constructor [] or the hash constructor {}
instead. Here's the right way to do the preceding broken
code fragments
for $i (1..10) {
@list = somefunc($i);
$LoL[$i] = [ @list ];
}
The square brackets make a reference to a new array with a
_c_o_p_y of what's in @list at the time of the assignment. This
is what you want.
Note that this will produce something similar, but it's much
harder to read:
for $i (1..10) {
@list = 0 .. $i;
@{$LoL[$i]} = @list;
}
Is it the same? Well, maybe so--and maybe not. The subtle
difference is that when you assign something in square
brackets, you know for sure it's always a brand new
30/Jan/96 Last change: perl 4
PERLDSC(1) Perl Programmers Reference Guide PERLDSC(1)
reference with a new _c_o_p_y of the data. Something else could
be going on in this new case with the @{$LoL[$i]}}
dereference on the left-hand-side of the assignment. It all
depends on whether $LoL[$i] had been undefined to start
with, or whether it already contained a reference. If you
had already populated @LoL with references, as in
$LoL[3] = \@another_list;
Then the assignment with the indirection on the left-hand-
side would use the existing reference that was already
there:
@{$LoL[3]} = @list;
Of course, this _w_o_u_l_d have the "interesting" effect of
clobbering @another_list. (Have you ever noticed how when a
programmer says something is "interesting", that rather than
meaning "intriguing", they're disturbingly more apt to mean
that it's "annoying", "difficult", or both? :-)
So just remember to always use the array or hash
constructors with [] or {}, and you'll be fine, although
it's not always optimally efficient.
Surprisingly, the following dangerous-looking construct will
actually work out fine:
for $i (1..10) {
my @list = somefunc($i);
$LoL[$i] = \@list;
}
That's because _m_y() is more of a run-time statement than it
is a compile-time declaration _p_e_r _s_e. This means that the
_m_y() variable is remade afresh each time through the loop.
So even though it _l_o_o_k_s as though you stored the same
variable reference each time, you actually did not! This is
a subtle distinction that can produce more efficient code at
the risk of misleading all but the most experienced of
programmers. So I usually advise against teaching it to
beginners. In fact, except for passing arguments to
functions, I seldom like to see the gimme-a-reference
operator (backslash) used much at all in code. Instead, I
advise beginners that they (and most of the rest of us)
should try to use the much more easily understood
constructors [] and {} instead of relying upon lexical (or
dynamic) scoping and hidden reference-counting to do the
right thing behind the scenes.
In summary:
30/Jan/96 Last change: perl 5
PERLDSC(1) Perl Programmers Reference Guide PERLDSC(1)
$LoL[$i] = [ @list ]; # usually best
$LoL[$i] = \@list; # perilous; just how my() was that list?
@{ $LoL[$i] } = @list; # way too tricky for most programmers
CCCCAAAAVVVVEEEEAAAATTTT OOOONNNN PPPPRRRREEEECCCCEEEEDDDDEEEENNNNCCCCEEEE
Speaking of things like @{$LoL[$i]}, the following are
actually the same thing:
$listref->[2][2] # clear
$$listref[2][2] # confusing
That's because Perl's precedence rules on its five prefix
dereferencers (which look like someone swearing: $ @ * % &)
make them bind more tightly than the postfix subscripting
brackets or braces! This will no doubt come as a great
shock to the C or C++ programmer, who is quite accustomed to
using *a[i] to mean what's pointed to by the _i'_t_h element of
a. That is, they first take the subscript, and only then
dereference the thing at that subscript. That's fine in C,
but this isn't C.
The seemingly equivalent construct in Perl, $$listref[$i]
first does the deref of $listref, making it take $listref as
a reference to an array, and then dereference that, and
finally tell you the _i'_t_h value of the array pointed to by
$LoL. If you wanted the C notion, you'd have to write
${$LoL[$i]} to force the $LoL[$i] to get evaluated first
before the leading $ dereferencer.
WWWWHHHHYYYY YYYYOOOOUUUU SSSSHHHHOOOOUUUULLLLDDDD AAAALLLLWWWWAAAAYYYYSSSS uuuusssseeee ssssttttrrrriiiicccctttt
If this is starting to sound scarier than it's worth, relax.
Perl has some features to help you avoid its most common
pitfalls. The best way to avoid getting confused is to
start every program like this:
#!/usr/bin/perl -w
use strict;
This way, you'll be forced to declare all your variables
with _m_y() and also disallow accidental "symbolic
dereferencing". Therefore if you'd done this:
my $listref = [
[ "fred", "barney", "pebbles", "bambam", "dino", ],
[ "homer", "bart", "marge", "maggie", ],
[ "george", "jane", "alroy", "judy", ],
];
print $listref[2][2];
The compiler would immediately flag that as an error _a_t
30/Jan/96 Last change: perl 6
PERLDSC(1) Perl Programmers Reference Guide PERLDSC(1)
_c_o_m_p_i_l_e _t_i_m_e, because you were accidentally accessing
@listref, an undeclared variable, and it would thereby
remind you to instead write:
print $listref->[2][2]
DDDDEEEEBBBBUUUUGGGGGGGGIIIINNNNGGGG
The standard Perl debugger in 5.001 doesn't do a very nice
jowing issue that came up last spring, but
couldn't find anything current. Was that resolved?
3) Finally, does anyone know of any 1999/ V8's on dealer lots. Will consider
2 or 4 wd/ regular or extra cab. Nevada/ No. Calif/ Idaho would be great.
Does anyone know how to check dealer inventory? My local dealer only wants
to order a 2000 for me.
Thanks for your help. I can't wait to order my new toy!
-Vic
This archive was generated by hypermail 2b29 : Fri Jun 20 2003 - 12:18:15 EDT