Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Your favourite gory detail...

by BrowserUk (Patriarch)
on Jun 20, 2004 at 23:20 UTC ( [id://368340]=note: print w/replies, xml ) Need Help??


in reply to Your favourite gory detail...

With regard to the "difference" between  \(@array, @brray) and \((@array), @brray)--there is none.

In both cases, the contents of both arrays get flattened into a single list. The backslash is then applied to each element of that list. No amount of extra parens will change the outcome, which is a list of references to the elements of boths arrays.

Update: My (least) favorite gory detail is the limitation of using prototypes to create your own map-like subs that I first encountered in Problem emulating built-ins like map and grep. and still get bitten by from time to time.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re: Your favourite gory detail...
by blokhead (Monsignor) on Jun 20, 2004 at 23:41 UTC
    Er.. did you try it?
    use Data::Dumper; @array = (1,2,3); @brray = (4,5,6); print Dumper [ \(@array,@brray) ]; print Dumper [ \((@array),@brray) ]; __END__ $VAR1 = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; $VAR1 = [ \1, \2, \3, [ 4, 5, 6 ] ];
    This is perl, v5.8.4 built for i386-linux-thread-multi

    Update: oops, this should be a follow-up to BrowserUk's reply...

    blokhead

Re^2: Your favourite gory detail...
by ihb (Deacon) on Jun 20, 2004 at 23:41 UTC

    Yes, there is a difference.

    \() is like a factored out reference operator before every thing inside the parenthesis, so \($foo, @bar, %baz, &frah) is (\$foo, \@bar, \%baz, \&frah), except if there's only one aggregate data type inside it. In that case it gets flattened and the backslash is then applied to each element of that list, as you say. This means that

    \((@foo), @bar)
    becomes
    (\(@foo), \@bar)
    which becomes
    (\($foo[0], $foo[1], ..., $foo[$#foo]), \@bar)
    and finally
    ((\$foo[0], \$foo[1], ..., \$foo[$#foo]), \@bar)

    Another example:

    \((@foo, @bar), @baz) (\(@foo, @bar), \@baz) ((\@foo, \@bar)), \@baz)

    Update: I just realized that my second example is rather useless as the end result is the same as if there was no inner parenthesis. But the intermediate step perhaps still has a pedagogical value.

    (I don't know if this recursive behaviour is how it's actually performed under the hood, but it works this way none the less.)

    ihb

Re^2: Your favourite gory detail...
by Ido (Hermit) on Jun 21, 2004 at 12:45 UTC
    That's why I said it was WEIRD!;)

    Update: Gha..That too was meant to follow BrowserUk's..

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-25 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found