Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by haukex (Archbishop)
on Oct 11, 2020 at 10:08 UTC ( [id://11122694]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^4: What's happening in this expression?
by zapdos (Sexton) on Oct 11, 2020 at 14:02 UTC
    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.

Log In?
Username:
Password:

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

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

    No recent polls found