On Tue, 2004-07-27 at 12:54, Daniel Allen wrote:
> LINE: while (defined($_ = <ARGV>)) {
>     /(\d+)/;
>     print "$1 ";
> }
> continue {
> #    print $_;
> }

The continue interposes another block into the loop.  According to
perlre as quoted by Steve below, $1 et al. are scoped until the end of
the enclosing block.  So when execution enters the continue block each
time through the loop, $1 is wiped clean.  Removal of the continue block
permits the previous value of $1 to persist into the next execution of
the while block.

However, if you remove the continue block and rework the loop to use
redo/last, $1 is still wiped on each loop:

  $ cat test-redo.pl
  while (1) {
      defined ($_ = <ARGV>) or last;
      /(\d+)/;
      print "\$1: $1\n";
      redo;
  }

  $ echo -e "9 \n \n 2" | perl test-redo.pl
  $1: 9
  $1:
  $1: 2

This I cannot explain.  :)


 -- Jeremy


> Coincidentally, my local 'mongers group is discussing a very similar
> situation.  So far, I'm not satisfied with perlre's explanation of how
> numbered variables are scoped, and the real situation seems to be
> confusing, too.
> 
> I started by forgetting an if (/whatever/) around my print, and wrote this:
> 
> $ echo -e "9 \n \n 2" | perl -ne'/(\d+)/; print "$1 ";'
> 
> This has the following result:
> 
> 9 9 2
> 
> ...instead of "9  2" as I'd originally expected.  OK, $1 is set to the
> last successful value.  I can deal with that.  The weird part is what
> happens under -p instead, and when you look at both after deparsing. 
> Deparsing this gives:
> 
> LINE: while (defined($_ = <ARGV>)) {
>     /(\d+)/;
>     print "$1 ";
> }
> 
> Changing my code from -n to -p : 
> 
> $ echo -e "9 \n \n 2" | perl -pe'/(\d+)/; print "$1 ";'
> 
> prints as if $1 were reset to undef on non-match:
> 
> 9 9
> 
> 2  2
> 
> This code deparses as:
> 
> LINE: while (defined($_ = <ARGV>)) {
>     /(\d+)/;
>     print "$1 ";
> }
> 
> continue {
>     print $_;
> }
> 
> If I use this code, commenting out the 'print $_;',  I get the answer
> I'd originally expected:
> 
> $ cat testperl.pl
> LINE: while (defined($_ = <ARGV>)) {
>     /(\d+)/;
>     print "$1 ";
> }
> continue {
> #    print $_;
> }
> 
> $ echo -e "9 \n \n 2"| perl testperl.pl
> 9  2
> 
> Can anybody tell me why?
> 
> -Daniel
> --
> http://kw.pm.org/ - Kitchener-Waterloo Perl Mongers -         [EMAIL PROTECTED]
> http://coder.com/ - Prescient Code Solutions - (519) 575-3733 [EMAIL PROTECTED]
> 
> 
> On Wed, 21 Jul 2004 13:04:25 -0400, Tolkin, Steve <[EMAIL PROTECTED]> wrote:
> > OK, here is the answer:
> > http://www.perldoc.com/perl5.6.1/pod/perlre.html says:
> > The numbered variables ($1, $2, $3, etc.) and the related punctuation
> > set ($+, $&, $`, and $') are all dynamically scoped until the end of the
> > enclosing block or until the next successful match, whichever comes
> > first.


_______________________________________________
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to