Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: What's happening in this expression?

by zapdos (Sexton)
on Oct 11, 2020 at 09:58 UTC ( [id://11122693]=note: print w/replies, xml ) Need Help??


in reply to Re: What's happening in this expression?
in thread What's happening in this expression?

Thank you so much. What are the two warnings "Useless use of a variable in void context at -e line 1." referring to?
  • Comment on Re^2: What's happening in this expression?

Replies are listed 'Best First'.
Re^3: What's happening in this expression?
by haukex (Archbishop) on Oct 11, 2020 at 10:08 UTC
    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.
        Please, why the statement my $a, $x, $y, $z = foo() is in void context? I don't get it.

        If you are unsure about Perl's concept of context, see e.g. the Context tutorial, the section "Context" in Chapter 2 of the Camel, or the section "Context" in Modern Perl.

        In the example I showed, perl -e 'my $a, $x, $y, $z = foo()', the statement is in void context because it is the only statement in the program and its return value is not being used anywhere.

        In my @foo = ( my $a, $x, $y, $z = foo() ); the statement is in list context because it is being assigned to an array, and in my $bar = ( my $a, $x, $y, $z = foo() ); it's in scalar context. For the last statement in a sub, the context of the caller is used, which is why in the above example, my @x = bar(); the statement my $a, $x, $y, $z = foo(); is in list context and in my $r = bar(); it's in scalar context. If you were to write my $a, $x, $y, $z = foo(); 1;, that would also force void context on the statement.

Re^3: What's happening in this expression?
by perlfan (Vicar) on Oct 11, 2020 at 10:09 UTC
    >then $x and $y are simply sitting there

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-25 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found