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

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

Replies are listed 'Best First'.
Re^7: Match operator giving unexpected output
by LanX (Saint) on Jan 13, 2015 at 23:48 UTC
    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

      actually you have proven how problematic the wording "list in scalar context" is:

      I disagree with that. scalar keyword that looks like a function is the real source of the problem here.

        What?

        You can easily replace all scalar with $a= in the demonstrated code, without any different outcome.

        Cheers Rolf

        PS: Je suis Charlie!