Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: What's happening in this expression? (Updated)

by LanX (Saint)
on Oct 11, 2020 at 12:42 UTC ( [id://11122699]=note: print w/replies, xml ) Need Help??


in reply to What's happening in this expression?

> but instead do:

>  my $a, $x, $y, $z = foo()

The precedence of = is higher than , !

Resulting evaluation:

(my $a), ($x), ($y),  ($z = foo())

That's effectively the same like 4 separate statements.

my $a ; $x ; $y ; $z = foo() ;

Consequences

1. => scalar assignment to $z and scalar context for foo() call

2. => my declaration only for $a

Update

3. => $x and $y are undeclared and in void context

4. => strict will fail

In short: DON'T!!!

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

Replies are listed 'Best First'.
Re^2: What's happening in this expression? (Updated)
by likbez (Sexton) on Oct 12, 2020 at 00:24 UTC
    What about
    my $a=$x=$y=$z = foo()
    Here my attribute does not propogate like in case of comma. It applies only to $a, right ?

    But now the fun starts

    DB<3> use v5.10
    
    DB<4> my $a=$x=$y=$z = 1
    
    DB<5> say "|$a|$x|$y|$z|"
    ||1|1|1|  
    DB<6> unless( defined($a) ){ say "a is undefined"}
    a is undefined
    
    why $a remains uninitialized?

      The debugger is a bad place to play with scoping like this. In effect when you evaluate single lines like this they're more like doing an eval within the scope of the program (more or less; I'm sure someone more familiar with the perl5db could give more specifics). It's kind of like (handwaving) textually shimming in say DebugDump( eval { YOURTEXTHERE } ) into wherever you're looking at and seeing the result.

      This means that your my declaration is happening inside of a transient scope (that single eval statement) and then it's going away. Since the my was affecting only $a when you check for defined-ness it fails because the package $a wasn't defined (however your modifications to $x et al changes the package versions of those and the values do persist after the statement).

      $ cat test.pl use 5.032; my $foo = 10; say qq{foo: $foo} $ perl -d test.pl Loading DB routines from perl5db.pl version 1.57 Editor support available. Enter h or 'h h' for help, or 'man perldebug' for more help. main::(test.pl:2): my $foo = 10; DB<1> x $foo 0 undef DB<2> n main::(test.pl:3): say qq{foo: $foo} DB<2> x $foo 0 10 DB<3> my $foo = 20 DB<4> x $foo 0 10 DB<5> my $foo = 20; say qq{foo: $foo} foo: 20 DB<6> x $foo 0 10

      Simple rule of thumb I tend to follow is just don't use my (or state or our) from the debugger command line to try and affect anything outside of that immediate command line.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        You are right. My variables are not always treated correctly, although recently the situation improved. I remember that in the past you just can't work with my variables at all. I just have a utility that stripped my moving them to tail comments and then reversed the situation. But now the usage of my "official" as it is forced by the strict pragma. Which means that such a situation is less acceptable.

        Also if you are using recursion my attribute can't be stripped at all. So this is a clear deficiently.

        That's sad, because IMHO the debugger is the crown jewel of Perl language environment and remains in certain areas unmatched by competition(macros, flexibility of c command, etc.) Possibility of b lineno ($var eq "value") is indispensable for debugging complex programs. That's what I always stress in my Perl advocacy efforts" "Unmatched by competition."

        So any deficiencies here are "highly undesirable."

        That's, of course, raises the question of development priorities...

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

      The comma is left associative and the assignment is right associative. See perlop.

      Here my attribute does not propogate like in case of comma.

      No, it doesn't "propagate" with the comma either, as I showed.

      That's not what I get nor what I would expect (but this is not in debug mode):

      Win8 Strawberry 5.8.9.5 (32) Sun 10/11/2020 21:47:54 C:\@Work\Perl\monks >perl -Mwarnings my $a=$x=$y=$z = 1; print "|$a|$x|$y|$z|"; ^Z |1|1|1|1| Win8 Strawberry 5.30.3.1 (64) Sun 10/11/2020 21:45:55 C:\@Work\Perl\monks >perl -Mwarnings use v5.10; my $a=$x=$y=$z = 1; say "|$a|$x|$y|$z|"; ^Z |1|1|1|1|

      Update:

      What about

      my $a=$x=$y=$z = foo()
      With respect to this specific statement:
      Win8 Strawberry 5.8.9.5 (32) Sun 10/11/2020 21:48:09 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings use Data::Dump qw(dd); sub foo { return 9, 8, 7; } my ($x, $y, $z); my $a = $x = $y = $z = foo; dd $a, $x, $y, $z; ^Z (7, 7, 7, 7)
      Same result under version 5.30.3.1.


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-23 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found