Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Your favourite gory detail...

by BrowserUk (Pope)
on Jun 20, 2004 at 23:20 UTC ( #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...


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)
    (\(@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.)


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?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2021-10-27 22:23 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (94 votes). Check out past polls.