On Mon, Jul 18, 2005 at 01:41:52AM -0700, avised @ kbcfp. com wrote:
> This test program:
> 
>     my $foo = 'foo';
>     for ($foo) {
>         m!(\w+)! or die;
>         print "\$1 has been set to: $1\n";
>     }
>     print "\$1 is now: $1\n";
> 
> gives the output:
> 
> $1 has been set to: foo
> $1 is now: 
> 
> Somehow, although $1 was set it has been changed back to undef on
> exiting the 'for' block.  I don't see anything in perlre or perlvar
> documenting this.

Feature, not bug.

>From perlre:

       The numbered match variables ($1, $2, $3, etc.) and the related punctu-
       ation set ($+, $&, $`, $', and $^N) are all dynamically scoped until
       the end of the enclosing block or until the next successful match,
       whichever comes first.  (See "Compound Statements" in perlsyn.)

$1 and friends have an implicit "local" on them so they are localized to
a block.  Its been this way for a long time.

$ perl5.4.5 -wle '$_ = "foo"; { /(\w)/;  print $1 } print $1'
f
Use of uninitialized value at -e line 1.


-- 
Michael G Schwern     [EMAIL PROTECTED]     http://www.pobox.com/~schwern
'All anyone gets in a mirror is themselves,' she said. 'But what you
gets in a good gumbo is everything.'
        -- "Witches Abroad" by Terry Prachett

Reply via email to