http://qs321.pair.com?node_id=443218

uksza has asked for the wisdom of the Perl Monks concerning the following question:

Hello wise monks,

use warnings; use strict; my @l = qw/10 20 30/; #my $t += $_ foreach @l; my $t; $t += $_ foreach @l; print $t;
I know - commented code doesn't works, but can You tell my why?

stay in peace,
Lukasz

Replies are listed 'Best First'.
Re: Counting variable initialization
by dragonchild (Archbishop) on Mar 29, 2005 at 18:58 UTC
    You're asking the wrong question, which is the problem. You're asking "How do I simultaneously declare a variable and use it within a loop to sum a list?" You really want to ask "How do I sum the values in a loop and assign the result to a variable?"
    use List::Util qw( reduce ); my $t = reduce { $a + $b } @list; # Or ... use List::Util qw( sum ); my $t = sum @list;

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Counting variable initialization
by tlm (Prior) on Mar 29, 2005 at 18:25 UTC

    Try -MO=Deparse to see what perl thinks of your code:

    $ perl -MO=Deparse 443218.pl my(@l) = ('10', '20', '30'); foreach $_ (@l) { my $t += $_; } print $t; 443218.pl syntax OK
    where file 443218.pl contains
    $ cat 443218.pl use warnings; use strict; my @l = qw/10 20 30/; my $t += $_ foreach @l; #my $t; #$t += $_ foreach @l; print $t;

    the lowliest monk

    Update: Added the contents of the file given to -MO=Deparse for clarification.

      Deparse is wrong in this case, on two counts: 1) print($t) is not strict safe in the deparsed version, but it is in the original, and 2) print($t) prints the package variable in the deparsed version, but not in the original.

      To answer the OP, my is considered a function for syntax purposes, so my ... foreach ...; means (my ...) foreach ...;, which makes no sense.

        I wondered about the fact that strict seemed to be violated. I defer to your interpretation, but it is a bummer to know that Deparse can be wrong, because I use it quite a bit. Sorry to the OP for the wrong info.

        the lowliest monk

      Thats odd. That isn't the deparse I get at all. Before:

      use strict; my @l = qw/10 20 30/; my $i += $_ foreach @l; my $t; $t += $_ foreach @l; print '$t = ' . $t . "\n"; print '$i = ' . $i . "\n";
      After:
      use strict 'refs'; my(@l) = ('10', '20', '30'); ; my $i += $_ foreach (@l); my $t; ; $t += $_ foreach (@l); print '$t = ' . $t . "\n"; print '$i = ' . $i . "\n";
      Output for both scripts:
      $t = 60 $i =


      ___________
      Eric Hodges
Re: Counting variable initialization
by ambs (Pilgrim) on Mar 29, 2005 at 18:24 UTC
    my receives a list of variables to initialize. Perl would first interpret the expression and then try to create a variable with the result. And that doesn't work that way.

    I think other monks can give wiser answers, but this is how I understand it.

    Alberto Simões