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

Re: Not understanding 2 sentences in perldoc

by choroba (Cardinal)
on Jul 29, 2020 at 20:13 UTC ( [id://11120010]=note: print w/replies, xml ) Need Help??


in reply to Not understanding 2 sentences in perldoc

List assignment:
my ($x, $y, $z) = qw( 1 2 3 );

List assignment in list context produces a list of lvalues:

(my ($x, $y, $z) = qw( 1 2 3 )) = qw( a b c ); # $x = 'a', $y = 'b', $ +z = 'c'.

List assignment in scalar context:

print scalar (my ($x, $y, $z) = qw( a b c d )); # 4

Modifying a scalar assignment:

($x = 12) =~ s/1/4/; print $x; # 42

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Not understanding 2 sentences in perldoc
by Anonymous Monk on Jul 29, 2020 at 20:23 UTC
    List assignment in list context produces a list of lvalues:

     (my ($x, $y, $z) = qw( 1 2 3 )) = qw( a b c ); # $x = 'a', $y = 'b', $z = 'c'.

    What is the lvalues here?
      > What is the lvalues here?

      ($x, $y, $z) are after the first assignment ready for the second one.

      Lvalue means left value of assignment (the recipient)

      See also perlglossary

      • lvalue

        Term used by language lawyers for a storage location you can assign a new value to, such as a variable or an element of an array. The “l” is short for “left”, as in the left side of an assignment, a typical place for lvalues. An lvaluable function or expression is one to which a value may be assigned, as in pos($x) = 10 .

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        "($x, $y, $z) are after the first assignment ready for the next one." The next one is the assignment to qw( a b c ); ?

        And what happens to qw( 1 2 3 ) in (my ($x, $y, $z) = qw( 1 2 3 )) = qw( a b c ); # $x = 'a', $y = 'b', $z = 'c'. ?

        haukex posted "The assignment my ($x, $y, $z) = qw( 1 2 3 ) happens first, i.e. those three variables are assigned the numbers 1 to 3, and this first assignment returns the lvalues, to which the values qw( a b c ) are immediately assigned."

        "and this first assignment returns the lvalues" What lvalues does it return? I see (my ($x, $y, $z) = qw( 1 2 3 )) = qw( a b c ); as # 1 = 'a', 2 = 'b', 3 = 'c'.

      # | inner assignment with a LHS (l) and RHS (r) # llllllllll..v...rrrrrrr # + $x = 1, $y = 2, $z = 3. (my ($x, $y, $z) = ( 1, 2, 3 )) = ('a', 'b', 'c' ); # llllllllll.................^..rrrrrrrrrrrrr # + $x = 'a', $y = 'b', $z = 'c'. # | outter assignment with a LHS (l) a +nd RHS (r)
      Slightly changed the internal scalar semantics by eliminating qw.
Re^2: Not understanding 2 sentences in perldoc
by perlfan (Vicar) on Jul 29, 2020 at 21:18 UTC
    For my own edificiation, would we expect B::Deparse to have broken this down further? This was my first attempt to decompose it.
    perl -MO=Deparse -e '(my ($x, $y, $z) = qw( 1 2 3 )) = qw( a b c )'
    (my($x, $y, $z) = ('1', '2', '3')) = ('a', 'b', 'c');
    -e syntax OK
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-24 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found