in reply to Re: 'my' problems
in thread 'my' problems

Hi Keith

Wrote a small program that will reproduce the problem.

#!/usr/bin/perl my $limitstart = 20; my $limitantal = 20; strange_sub(limitstart => $limitstart, limitantal => $limitantal); strange_sub(); sub strange_sub { my (%params) = @_; my $string = 'limit ' . $params{'limitantal'} if ((!$params{'l +imitstart'}) && ($params{'limitantal'})); $string = 'limit ' . $params{'limitstart'} . ", " . $params{'l +imitantal'} if (($params{'limitstart'}) && ($params{'limitantal'})); print STDERR "limitstring = " . $string . "\n"; }

/jocke

Replies are listed 'Best First'.
Re^3: 'my' problems
by reasonablekeith (Deacon) on May 23, 2005 at 11:36 UTC
    I'm surprised. :-o

    You have your answers below, but I'd thought I'd post a simple example, which shows quite explictly that the 'my' does scope a variable, but doesn't re-initialise it if you have a conditional which evaluates to false.

    print count() . "\n"; print count() . "\n"; print count() . "\n"; sub count { my $counter = 'whatever' if 0; # declare local 'static' variable $counter++; } __OUTPUT__ 0 1 2

    PS. Don't actually do this though

    ---
    my name's not Keith, and I'm not reasonable.