I am trying to fix a very old script, which is trying to find a string FILE_DESC_LIMIT
in a text file and change it to something else.
For this my current script which is working fine on Perl 5.10 I see it uses a regular expression
qr!^FILE_DESC_LIMIT=\d+(\s*)!;
But this is not working in Perl 5.16 , so I tried to print this expression. In 5.10 I see
(?-xism:\bFILE_DESC_LIMIT=\d+(\s*))
While in Perl 5.16 i see
(?^:^FILE_DESC_LIMIT=\d+(\s*))
I see qr
was adding -xism
while it not adding it in 5.16, why there is a difference here?
If I add it explicitly in my regular expression, it works. But I doubt the solution as Perl doesn't add it explicitly there might be some problem here.
Is there some different way which I should use for Perl 5.16? Why has Perl changed this behavior?
my code looks like this , we are creating a regex and then using regex we are fomulating an expression which is then evaluated on an open file, then the evaluated output is written to the same file.
my atempRegex=qr!^FILE_DESC_LIMIT=\d+(\s*)!;
my $replaceSting => qq!FILE_DESC_LIMIT=$FileDescLimit\$1!
$tempRegex =~ s!\^!\\b!g if ($] =~ /5\.010/);
my $code .= "\$File =~ s!$tempRegex!$replaceSting!gsm;\n";
open (FH, "<$WORKING_DIR/$base_script_name.$$.tmp") || logWrite2(E,"--
ERROR: Unable to open $WORKING_DIR/$base_script_name.$$.tmp for reading
($!)");
my $File = join("",<FH>);
close (FH);
eval $code;
open (FH, ">$WORKING_DIR/$base_script_name.$$.tmp") || logWrite2(E,"--
ERROR: Unable to open $WORKING_DIR/$base_script_name.$$.tmp for writing
($!)");
print FH $File;
close (FH);
change which i did to make it work in perl 5.16 is , i added the -xism modifiers explicitly into the regex and also modified the \b modifier condition
my $FileDescLimit=2048;
my $tempRegex=qr!^FILE_DESC_LIMIT=\d+(\s*)!;
my $replaceSting => qq!FILE_DESC_LIMIT=$FileDescLimit\$1!
if ($] >=5.016)
{
$tempRegex=~ s!\^!-xism!;
}
$tempRegex =~ s!\^!\\b!g if ($] >=5.010);
my $code .= "\$File =~ s!$tempRegex!$replaceSting!gsm;\n";
open (FH, "<$WORKING_DIR/$base_script_name.$$.tmp") || logWrite2(E,"-- ERROR: Unable to open $WORKING_DIR/$base_script_name.$$.tmp for reading ($!)");
my $File = join("",<FH>);
close (FH);
eval $code;
open (FH, ">$WORKING_DIR/$base_script_name.$$.tmp") || logWrite2(E,"-- ERROR: Unable to open $WORKING_DIR/$base_script_name.$$.tmp for writing ($!)")
print FH $File;
close (FH);
i dont know if the problem is because of -xism modifier or because of \b modifier, as i see the script was modified on version 5.10 and \b modifier was added explicitly, may the real problem is with this modifier.
referring to one of the answer below that ?^ is equivalent to ?-xism , i removed the code
if ($] >=5.016)
{
$tempRegex=~ s!\^!-xism!;
}
from my script , so -xism is not part of my regex anymore , and i have added \b in my regex as done for perl version 5.10 , below code replaces the second caret sign in my regex , (?^:^FILE_DESC_LIMIT=\d+(\s*)) with \b, with the resultant regex (?^:\bFILE_DESC_LIMIT=\d+(\s*)) , the string is matched and replaced
if ($] =~ /5\.016/)
{
my $n=0;
$Source =~ s/\^/++$n==2?"\\b":"\^"/ge
}
as i see in regex tutorials \b is used to define word boundary and to perform a whole word match , i am not clear why it is not able to match FILE_DESC_LIMIT=2048 string in the file without \b.
The documentation on Extended Patterns in perlre
has this
Starting in Perl 5.14, a
"^"
(caret or circumflex accent) immediately after the"?"
is a shorthand equivalent tod-imnsx
. Flags (except"d"
) may follow the caret to override it. But a minus sign is not legal with it.
Of those modifiers, -imsx
matches what you saw in v5.10, while -n
enables the capturing behaviour of parentheses and d
enables "default" character set behaviour. These last two are new modifiers
None of those are any different from the values you show in v5.10, so you will have to show your data and exactly what you wrote to get a change in behaviour
See Regular Expressions in Perl 5.14 Delta: the (?^
now signifies default modifiers. It's just a matter of how the compiled regular expressions are serialized, the more important is the difference between \b
and ^
in your regular expressions.
来源:https://stackoverflow.com/questions/45417696/qr-operation-in-perl-5-16