philgoetz has asked for the wisdom of the Perl Monks concerning the following question:

What does this do?

@x = (a => b => c);

There's a lot of that in the program I'm looking at. As far as I can tell, it's the same as

@x = (a, b, c);

Replies are listed 'Best First'.
Re: What is (a => b => c) ?
by LanX (Saint) on Mar 26, 2015 at 20:02 UTC
    > As far as I can tell, it's the same as @x = (a, b, c);

    It's the same as @x = ("a", "b", c);

    please note that c is not quoted, hence this shouldn't compile under strict

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    PS: Je suis Charlie!

Re: What is (a => b => c) ?
by jeffa (Bishop) on Mar 26, 2015 at 20:45 UTC

    Now that you know what it does, here is what it probably should be:

    my @x = qw( a b c );

    or perhaps even:

    my @x = 'a' .. 'c';

    And remember, testing is fun! Cheers!

    perl -MTest::More=no_plan -e'is_deeply [a=>b=>c],[qw(a b c)]' perl -MTest::More=no_plan -e'is_deeply [a=>b=>c],["a".."c"]'

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: What is (a => b => c) ?
by Anonymous Monk on Mar 26, 2015 at 19:38 UTC
Re: What is (a => b => c) ?
by Anonymous Monk on Mar 26, 2015 at 20:05 UTC

    Actually, to be more accurate: You are only correct if strict is off. But since everyone uses strict (right? ;-) ), @x = (a => b => c); is like @x = ("a", "b", c); (note the quotes), and the final "c" also needs to be quoted unless it's a function call.

      Doesn't it need to be &c to be a function call?

        Not if you declare/define it first:

        no strict; sub c { print "see?\n" } @x = ( a => b => c );

        or

        no strict; sub c; @x = ( a => b => c ); sub c { print "see?\n" }

        But not:

        no strict; @x = ( a => b => c ); sub c { print "see?\n" }

        UPDATE: corrected 2nd example (used &c; which called the sub ... oops)

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        

        Not at all.

        Prepending & to a sub call causes the sub call to ignore the sub's prototype.

        Prepending & to a sub call with no parameter list specified (not even ()) causes the sub call to ignore the sub's prototype, and forgoes the creation of a localized @_.

        These are not thing you should ever need to do. Because it must be followed by a sub call, & has the side effect of forcing the following word to be treated as the name of a sub, but that's just a side effect.