Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

RE: A minor epiphany with grep and map

by chip (Curate)
on Jul 27, 2000 at 03:16 UTC ( #24594=note: print w/replies, xml ) Need Help??


in reply to A minor epiphany with grep and map

By the way, I'm intersted in an examination of @$x->{y}. Examining the code generated with -Dx, I think this runs only by accident. The left side of -> is supposed to be a reference, but in that code, the left hand side is an array, namely, @$x. And it's not a syntax error to have one of those on the left side of ->{y} because of pseudohashes.

Or so it seems.

--Chip Salzenberg, Free-Floating Agent of Chaos

Replies are listed 'Best First'.
RE: RE: A minor epiphany with grep and map
by mwp (Hermit) on Jul 27, 2000 at 03:57 UTC
    @$self->{ITEMS} @$x->{y}

    $self->{ITEMS} (or $x->{y}) returns a reference or pointer to an array of values. The @-operator dereferences it to be used by the map and grep functions.

    This could have been much more clearly written as:

    @{ $x->{y} }

    But if wishes were fishes... :) My point is that there's nothing magical about this, and doesn't really require an "examination."

    Hope this helps, or at least makes sense with all that chaos out there.

    Alakaboo
    I am the lurker that spams in the night.

    (UPDATE: I understand what chip is saying, and I'm not trying to be a smartass. I think he's reading into the code a little too deeply. Just wanted to clarify things for some of the less experienced readers. :P)

      I'm sorry, Alakaboo, you didn't understand what I wrote at all.

      Given the precedence rules, @$x->{y} is not the same as @{$x->{y}}. Rather, it's the same as (@$x)->{y}. Look at the output of -Dx and -Dts if you don't believe me.

      In fact, now that I try a simple test, the original code can't work:

      $ perl -le '$x={y=>[1]}; print @$x->{y}' Not an ARRAY reference at -e line 1. $ perl -le '$x={y=>[1]}; print @{$x->{y}}' 1

      So let's see the full code to the original, OK?

          -- Chip Salzenberg, Free-Floating Agent of Chaos

        No, I understood *sigh* I'm just an idiot... I was under the impression that @$x->{y} would work and not die. I guess I gave Perl too much of a benefit of the doubt.

        My apologies Chip, it was good of you to point that out. I hope that *I* didn't come across as being snippy.

        Best regards,

        Mike

RE: RE: A minor epiphany with grep and map
by ase (Monk) on Jul 27, 2000 at 13:31 UTC

    Hmmmm.... I see your point. @{$x->{y}} _is_ probably a better way to clarify this to the parser(and to the poor soul who has to maintain the code somewhere down the line- probably me.)

    Looking at perlop, I'm a little confused as to _why_ it works myself. Quoting from the Docs: (with my comments not in <code> tags)

    The Arrow Operator ``->'' is an infix dereference operator, just as it is in C and C++. If the right side is either a [...], {...}, or a (...) subscript,
    ..It seems this describes the situation..
    then the left side must be either a hard or symbolic reference to an array, a hash, or a subroutine respectively. (Or technically speaking, alocation capable of holding a hard reference, if it's an array or hash reference being used for assignment.) See the perlreftut manpage and the perlref manpage. Otherwise, the right side is a method name or a simple scalar variable containing either the method name or a subroutine reference, and the left side must be either an object (a blessed reference) or a class name (that is, a package name). See the perlobj manpage.
    It seems somehow the -> is binding before the @ in @$x, contrary to what I see in perlop
    I guess I should consider it a _feature_.
    Thank you, chip for noticing something that I completely glossed over. Good brain food.
    -ase

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://24594]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2020-07-07 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?