in reply to Re^3: Match operator giving unexpected output
in thread Match operator giving unexpected output

> What confusion where (where is the confusion)?

in the following you (could) see a flattened "list", but scalar doesn't return the last element, which should be 5.

DB<118> @a=(4,5) => (4, 5) DB<119> scalar (1,2,3,@a) => 2

in reality there is only the scalar comma operator returning the scalar of the last expression, which is here the size of @a.

Cheers Rolf

PS: Je suis Charlie!

Replies are listed 'Best First'.
Re^5: Match operator giving unexpected output
by Anonymous Monk on Jan 13, 2015 at 23:29 UTC
    LanX I think you just found a perl bug
    $ perl -e"@f=4..5;print scalar( 1,2,3,@f) " 2 $ perl -e"@f=4..5;print scalar( 1,2,3,) " 3
      Nevermind the warnings warn and not using numbers is clear
      $ perl -we"@f=( qw/ r p n /);print scalar( q/a/, q/b/, q/c/ ,@f) " Useless use of a constant ("a") in void context at -e line 1. Useless use of a constant ("b") in void context at -e line 1. Useless use of a constant ("c") in void context at -e line 1. 3
        actually you have proven how problematic the wording "list in scalar context" is:

        the scalar comma operator is more like a "weak" semicolon, i.e. separating evaluated expressions without terminating a statement.

        you'll sometimes see people writing stuff like

        DB<119> $a=0 => 0 DB<120> while ($a++,$a<5) {print "$a\n"} 1 2 3 4

        while only excepts one statement, so no semicolon possible.

        But every comma separated part is executed, but only the last result is returned

        so in your case, you'll get a warning, cause "a" is executed but never returned.

        DB<108> use warnings; ("a","b") => ("a", "b") DB<109> use warnings; scalar ("a","b") Useless use of a constant (a) in void context at (eval 34)[multi_perl5 +db.pl:2279] line 1.

        Cheers Rolf

        PS: Je suis Charlie!

        edit

        maybe it's best to say that scalar comma list is kind of a poor man's do block, but called in scalar context.

        update

        DB<135> sub ctx {print wantarray ? 'list' : defined wantarray ? 'sca +lar' : 'void'; return} DB<136> @a= do {ctx();ctx()} # list-do voidlist DB<137> do {ctx();ctx()};1 # void-do voidvoid DB<138> $a=do {ctx();ctx()} # scalar-do voidscalar DB<140> scalar (ctx(),ctx()) # scalar comma voidscalar DB<141> 1 while (ctx(),ctx()) # scalar (boolean) comma voidscalar