Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: What's happening in this expression?

by haukex (Bishop)
on Oct 11, 2020 at 09:50 UTC ( #11122691=note: print w/replies, xml ) Need Help??


in reply to What's happening in this expression?

Can you monks please explain step-by-step what computations will happen here?

Under strict, none, because it will not compile unless $x, $y, and $z have been previously declared. Otherwise, B::Deparse helps:

$ perl -MO=Deparse,-p -e 'my $a, $x, $y, $z = foo()' (my($a), $x, $y, ($z = foo()));

In other words, it's declaring a lexical $a, then $x and $y are simply sitting there, and then $z is assigned the return value of the function call foo() - although if that's not defined, that call will fail. See also Comma Operator. Turning on warnings gives some relatively helpful hints:

$ perl -w -e 'my $a, $x, $y, $z = foo()' Parentheses missing around "my" list at -e line 1. Useless use of a variable in void context at -e line 1. Useless use of a variable in void context at -e line 1. Name "main::x" used only once: possible typo at -e line 1. Name "main::z" used only once: possible typo at -e line 1. Name "main::y" used only once: possible typo at -e line 1. Undefined subroutine &main::foo called at -e line 1.

Replies are listed 'Best First'.
Re^2: What's happening in this expression?
by zapdos (Sexton) on Oct 11, 2020 at 09:58 UTC
    Thank you so much. What are the two warnings "Useless use of a variable in void context at -e line 1." referring to?
      What are the two warnings "Useless use of a variable in void context at -e line 1." referring to?

      They're referring to $x and $y, because the statement as written above is in void context, so these variables are in void context too. I was intentionally a little vauge with my "simply sitting there" because it's possible for them to not be in void context, for example if this statement was the last thing in a sub (the context that the sub is called in is passed through):

      use warnings; use strict; my ($x,$y,$z); sub foo { qw/a b c d/ } sub bar { my $a, $x, $y, $z = foo() } my @x = bar(); my $r = bar();

      This only produces the warning "Parentheses missing around "my" list". @x will contain the values (undef, undef, undef, "d"), because $a, $x, and $y are undef (nothing is assigned to them), while $z is assigned the return value of foo(), which means it is assigned "d" because my example sub foo is returning a list, and a list in scalar context evaluates to its last value. $r will contain "d" for the same reason.

        Please, why the statement my $a, $x, $y, $z = foo() is in void context? I don't get it.
      >then $x and $y are simply sitting there

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2020-11-26 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?