So why would you call a reference to an array instead of just calling the
array itself?
-----Original Message-----
From: Peter Scott [mailto:[EMAIL PROTECTED]]
Sent: Thursday, June 14, 2001 8:30 PM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: Re: Can't understand Reference interpretation
At 06:04 PM 6/14/01 -0500, [EMAIL PROTECTED] wrote:
>Gurus,
Grasshopper,
> The Camel, ( 3rd Ed. ), says,
>---------------------------------------------------------------------------
------------------
>$listref->[2][2] = "hello"; # pretty clear
>$$listref[2][2] = "hello"; # A bit confusing
>
>This second of these statements may disconcert the C programmer, who is
>accustomed
>to using *a[i] to mean "what's pointed to by the ith element of a". But in
>Perl, the five characters ($ @ * % &) effectively bind more tightly than
>braces or brackets. Therefore, it is $$listref and not $listref[2] that is
>taken to be a reference to an array.
>---------------------------------------------------------------------------
------------------
>
>Now, here is how I understand the first LHS is interpreted by perl:
>Rewriting, the first,
>$listref->[2]->[2] = "hello"; # Adding the redundant arrow ( for
>understanding )
>
>listref ------- is a scalar which is a reference to some array's 3rd
>elem
Nope, that there is a bareword that has no meaning in that code.
>listref->[2] ------- is a scalar which is a reference to some
>array's 3rd elem
>$listref->[2]->[2] ------- is a scalar lvalue which is assigned string
>"hello".
Try this instead:
$listref ----- is a scalar which is a reference to an array.
$listref->[2] ------- is the third element of that array. It also happens
to be a reference to another array.
$listref->[2]->[2] ------- is the third element of that other array. The
second arrow can be omitted through syntactic sugaring.
>Now, the second LHS:
>
>$$listref[2]->[2] = "hello"; # Adding the redundant arrow ( for
>understanding )
>
>$$listref[2] is interpreted as "$listref" is a reference to an array **
>and ** $$listref[2] is
> $ { $listref } [2]
>( i.e. given some array @k, and $refk = \@k, then $$refk[i] is $k[i].
>"Literal k always replaceable by $refk". )
>
>Why does the text say "$$listref" is a reference???
This is somewhat poorly worded. $$listref is not a reference to an array,
because for $$listref to mean something $listref would have to be a
reference to a scalar. It's saying that you shouldn't think that it's
interpreted as
${$listref[2]}[2]
because it isn't. But IMHO it would make more sense if it said $listref
instead of $$listref in the text there.
>One reference is
>"$listref" and the second is "$$listref[2]" ( equivalently $ { $listref }
>[2] ).
>
>I feel there is some gap in my understanding.
You seem to have it down.
There are two rules for understanding dereferencing (aside from the arrow
operator):
1. The name portion of a variable (the "word" if you want, that comes after
the initial punctuation sigil and before any more characters like [ or {
that signify an element) may be replaced by a simple scalar variable ($x,
not $x{foo} or $x[42], just $x, for some word x) which must contain a
reference to the way the variable is being used, e.g.
$$x - $x must contain reference to scalar
$$x[2] - $x must contain reference to array
2. The name portion may be replaced by a block yielding the appropriate
type of reference. This is for when you don't have a simple scalar
containing your reference. So, eg:
${func(42)} - func(42) must return reference to scalar
${$hash{ish}}{urp} - $hash{ish} must return reference to hash
--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com