S
sln
I'm probably going to use some wrong terms here but I
hope to give enough detail that I can get a definative
resolution to this, once and for all.
Basically I'm writing a sub that wants to take a regular
expression as a parameter. It then blindly operates on data,
matching, and posible substitution.
Apparently qr// will only function on the matching side, something like this:
# works
$rx = qr/\Q$sometext\E/s;
$data =~ /$rx/;
# or $data =~ $rx/
But this:
# does not work, no way no how
$rx = qr{s/\Q$sometext\E/junk/g};
$data =~ $rx;
Even though qr{s/\Q$sometext\E/junk/g} will pass warnings and errors,
even though the substitution is constant (ie, no runtime $1,$2, etc..)
it never matches.
I mean I could see a failure scenario if using $1.. on the substitution side
because it breaks undefined'ness, but if its given a constant it should work IMO.
And if it does compile, like the above does, it should work.
The fall back is to use an eval "" where something like this is possible:
$rx = "s/\\Q$sometext(.*?)\\E/junk\$1/g";
$expression = "\$res = \$data =~ $rx";
eval $expression;
if ($res) {
...
}
But eval is 2 to 4 times slower.
They only thing "dynamic" about the regualar expression above is the case of
substitution of $1.. Surely this could be taken into account when say using
the qr// construct couldn't it? Is it really breaking the rules, or would it
factor down to an eval anyway in that case? But the constant substitution,
I don't see why that can't work.
Is there anyway possible the substitution side will work?
TIA,
sln
hope to give enough detail that I can get a definative
resolution to this, once and for all.
Basically I'm writing a sub that wants to take a regular
expression as a parameter. It then blindly operates on data,
matching, and posible substitution.
Apparently qr// will only function on the matching side, something like this:
# works
$rx = qr/\Q$sometext\E/s;
$data =~ /$rx/;
# or $data =~ $rx/
But this:
# does not work, no way no how
$rx = qr{s/\Q$sometext\E/junk/g};
$data =~ $rx;
Even though qr{s/\Q$sometext\E/junk/g} will pass warnings and errors,
even though the substitution is constant (ie, no runtime $1,$2, etc..)
it never matches.
I mean I could see a failure scenario if using $1.. on the substitution side
because it breaks undefined'ness, but if its given a constant it should work IMO.
And if it does compile, like the above does, it should work.
The fall back is to use an eval "" where something like this is possible:
$rx = "s/\\Q$sometext(.*?)\\E/junk\$1/g";
$expression = "\$res = \$data =~ $rx";
eval $expression;
if ($res) {
...
}
But eval is 2 to 4 times slower.
They only thing "dynamic" about the regualar expression above is the case of
substitution of $1.. Surely this could be taken into account when say using
the qr// construct couldn't it? Is it really breaking the rules, or would it
factor down to an eval anyway in that case? But the constant substitution,
I don't see why that can't work.
Is there anyway possible the substitution side will work?
TIA,
sln