in reply to Re^3: wantarray - surprise behaviour
in thread wantarray - surprise behaviour

It's useful to think of the () as an operator creating an anonymous list.

No it isn't. () is just about getting things to parse correctly. The list comes from the comma operators , and =>.

Replies are listed 'Best First'.
Re^5: wantarray - surprise behaviour
by BrowserUk (Patriarch) on Aug 31, 2004 at 22:46 UTC

    I've seen this said before, but

    P:\test>perl -MO=Deparse -e"@a = 1,2,3,4,5; print @a" @a = 1, '???', '???', '???', '???'; print @a; -e syntax OK P:\test>perl -e"@a = 1,2,3,4,5; print @a" 1 P:\test>perl -MO=Deparse -e"@a = (1,2,3,4,5); print @a" @a = (1, 2, 3, 4, 5); print @a; -e syntax OK P:\test>perl -e"@a = (1,2,3,4,5); print @a" 12345

    Empirically, that suggests that the parens are, at least in some cases, required to build a list.


    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, algorithm, algorithm on the code side." - tachyon
      But they don't create lists. They are 'required' just as they are in some cases required to call a function correctly. That doesn't mean parens call functions. In the case you describe, parentheses are used for precedence:
      perl -MO=Deparse,-p -e"@a = 1,2,3,4,5; print @a" ((@a = 1), '???', '???', '???', '???'); print(@a); -e syntax OK
      A list is still created - it's just not the list you want.

        Which goes to demonstrate that saying that , and => builds lists is not enough.

        Just as parens are often required to clarify the programmers intent, so mentioning parens, and precedence is required to clarify the language's definition of a list.


        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, algorithm, algorithm on the code side." - tachyon
Re^5: wantarray - surprise behaviour
by Anonymous Monk on Sep 01, 2004 at 14:55 UTC
    No, the list *doesn't* come from the comma. It comes from the assignment:
    sub foo { $c = wantarray; print $c ? "A" : defined $c ? "S" : "V" } (foo(), foo(), foo()); print "\n"; $v = (foo(), foo(), foo()); print "\n"; @v = (foo(), foo(), foo()); print "\n"; __END__ VVV VVS AAA

      Well, no. That's a case of a list in scalar or list context. I quote from perlop about the assignment operator.

      a list assignment in list context produces the list of lvalues assigned to, and a list assignment in scalar context returns the number of elements produced by the expression on the right hand side of the assignment.