Steve Grazzini wrote:
> On Mon, Jul 21, 2003 at 10:34:59AM -0400, Jeff 'japhy' Pinyan wrote:
> > On Jul 21, Ed Christian said:
> >
> > > I assumed that, $1 would be reset (either undefined or set to
> > > null) once I exited the scope of the while loop my regexp was
> > > called in. Sadly, I was mistaken. :) Below is a test example of
> > > code I wrote, with $1 values differing from those I expected. Do
> > > I need to explicitly set $1..$n to an empty string before every
> > > regexp if I'm to test based on the results of that regexp?
> >
> > You assumed wrong. :)
> >
> > The documentation states that the $DIGIT variables are set after
> > each SUCCESSFUL pattern match. If a regex fails to match, the
> > $DIGIT variables retain their previous value.
>
> The docs also claim that the digit variables will be "dynamically
> scoped to the current BLOCK", which isn't quite accurate, and is
> presumably what confused Ed.
The docs are correct. $1 is dynamically scoped to the block. Ed's code
didn't leave the block, so this dynamic scoping didn't apply.
Example:
"bar" =~ /(bar)/;
print "before block: \$1=$1\n";
while (1) {
"foo" =~ /(foo)/;
print "inside block: \$1=$1\n";
last;
}
print "after block: \$1=$1\n";
Prints:
before block: $1=bar
inside block: $1=foo
after block: $1=bar
Thus, $1 is saved before entry to the block and restored afterward.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]