On Mon, Mar 10, 2003 at 12:24:51AM +0000, Richard Smith wrote:
> 65: $text =~ s{ <!--field--> ( .*? ) <!--end--> }
> 66: { &selection( $1 ) }gsex;
>
> ..
>
> 83: sub selection {
> 84:
> 85: my ( $name, $checked );
> 86: my $text = shift;
> 87: my $modified_text;
> 88:
> 89: for ( split /\n/, $text ) {
> 90:
> 91: {
> 92: m/name\s*=\s*"*(\w+)"*/;
> 93: $1 and $name = $1;
> 94: }
> The problem I seem to be having is on line 92/93. If the RE matches a
> value for name='string", then I want that string to be stored in the
> variable $name. If no match is found, I want the previous value of $name
> to persist.
>
> However, it seems that if the pattern on line 92 doesn't match, then
> $name inherits the value from the instance of $1 generated in the RE on
> line 66. I have tried enclosing the second RE in braces (as shown), but
> the previous value of $1 still seems to persist.
$1 et al. contain the values from the last *successfully matched* regular
expression. Thus, if the regex on line 92 does not match, $1 will still
have its value from line 65.
I would suggest writing lines 92 and 93 like this:
m/name\s*=\s*"?(\w+)"?/ and $name = $1;
Ronald