On 06/21/2007 10:34 AM, Mario D'Alessio wrote:
> Here's an example script:
>
> my $a = '1 foo';
> my $b = 'foo bar';
>
> if ( $a =~ /^\d+\s*(\w+)/ ) # line 1
> {
> $b =~ s/bar/$1/; # line 2
> }
> print $b, "\n";
>
> I am using the capture buffer in line 1 in the regex in line 2. But it
> doesn't
> work. The capture buffer is cleared before the $1 substitution can take
> place.
>
> I would have thought that Perl would perform the $1 substitution in the line
> 2
> before it "recognizes" the substitution command and clears the capture
> buffers.
>
> Please explain the order in which Perl processes line 1.
>
Line 1 is not the issue, and it works as you expect it. Line 2 is the
problem; when a successful match occurs, the match variables are
set/reset to new values.
> Do I have to "eval" line 2 to get it to work the way that I want without
> having
> to assign $1 to a temp var?
>
> Thanks.
>
> Mario
>
>
>
Using eval() is worse than just assigning to a temporary variable.
|