As somebody else pointed out: s/$regexp/whatever/
If you need the whatever to be evaluated early as well, you could build a
sub
my $regexp = qr/.../;
my $replace = ...;
my $replacer = sub { s/$regexp/$replace/; };
Then in your loop:
$_ = whateverstringneedsreplacing;
$replacer->();
The result of the substitution is in $_.
You could also do something cleaner with parameters:
my $replacer = sub { $_[0] =~ s/$regexp/$replace/; return $_[0] };
Then in the loop:
my $subresult = $replacer->($whateverstringneedsreplacing);
Note that you could use
$replacer = eval "sub {...}" ;
instead of
$replacer = sub { ... } ;
so that perl compiles the regex only once. Not entirely if it would speed it
up.
-Mathieu
I'm looking for a way to build a substitution (s///) once at the
> beginning of my program so that I don't have to evaluate a conditional
> and build the substitution each of the thousands of times my program
> loops. I know about the $regexp = qr/pattern/ construct, but can't
> figure out how to do something equivalent with a substitution.
>
> -Brian
>
>
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs