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. > > and 5.8.4 is the same except adding $^N (whatever that is). > > So it is not possible in Perl 5. > > Note that these numbered variables are somewhat like > global variables, and go do "action at a distance". > > Is there going to be a way in perl 6 to control this better? > > > Steve > > > > -----Original Message----- > From: Greg London [mailto:[EMAIL PROTECTED] > Sent: Wednesday, July 21, 2004 12:30 PM > To: Tolkin, Steve > Cc: [EMAIL PROTECTED] > Subject: RE: [Boston.pm] I want a 'compile time' check on missing parens > in regex > > Tolkin, Steve said: > > What is the scope of $1 and when does it get reset? > > here's a start: > http://www.greglondon.com/iperl/html/iperl.html#20_5_2_Capturing_parenth > eses_not_capturing > > I suppose I should make a note to include some s/// examples... > > note to self: self, add some s/// examples. > > -- > "Impatient Perl" > A GNU-FDL training manual for the hyperactive. > Free HTML/PDF downloads at greglondon.com/iperl > Paperback/coilbound available for $8.50+s&h > > _______________________________________________ > Boston-pm mailing list > [EMAIL PROTECTED] > http://mail.pm.org/mailman/listinfo/boston-pm > _______________________________________________ Boston-pm mailing list [EMAIL PROTECTED] http://mail.pm.org/mailman/listinfo/boston-pm