in reply to 'my' headache...why doesn't this work?

The left side of the for-Statement is executed multiple times, so with my, $columnsum seems to become a local variable in the for-block, although use strict doesn't complain.
Try
my $columnsum = 0; $columnsum += $_->{columns} for @data; print $columnsum;
and everything seems to be fine.

Replies are listed 'Best First'.
Re: Re: 'my' headache...why doesn't this work?
by S_Shrum (Pilgrim) on Apr 05, 2002 at 10:11 UTC

    That's works....I guess I understand the FOR issue. Still would'a thunk that strict would have gagged on it.

    ======================
    Sean Shrum
    http://www.shrum.net

      strict is smart enough to detect variables that are not properly declared, but not necessarily to detect variables that are logically misused. In this case, declaring a 'my' variable inside a for loop is perfectly legal (in fact, I do it all the time without thinking much about it), e.g.
      foreach my $foo (@ary) { . . . }
      But for a running summation, of course it's not what you want. So strict doesn't see a problem; although it's misused, the variable is properly declared.