Regex resetting the capture buffer

M

Mario D'Alessio

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.

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
 
A

anno4000

Mario D'Alessio said:
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.

Do you mean "line 2"?

Perl delays substitution in the replacement string until after the
match. It *must* do so for constructs like

s/x(.)y/y$1x/;

to work. The match must have happened before $1 has its intended value.
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?

How would "eval" help? You can use $_ for a temporary variable:

if ( $a =~ /^\d+\s*(\w+)/ ) # line 1
{
$b =~ s/bar/$_/ for $1; # line 2
}
print $b, "\n";

Anno
 
M

Mumia W.

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.
 
M

Mario D'Alessio

Please explain the order in which Perl processes line 1.
Do you mean "line 2"?

Yes. I just typed a typo.


Thanks for all the reponses. I now fully understand.

Mario
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top