Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Counting variable initialization

by uksza (Monsignor)
on Mar 29, 2005 at 18:20 UTC ( #443218=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on Counting variable initialization
Download Code
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

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 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://443218]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-09-17 04:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (57 votes), past polls