Perl 5.16中的qr操作 [英] qr operation in perl 5.16

查看:340
本文介绍了Perl 5.16中的qr操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修复一个非常旧的脚本,该脚本试图在文本文件中找到字符串FILE_DESC_LIMIT并将其更改为其他名称.

为此,我当前的脚本在Perl 5.10上运行良好,我看到它使用了正则表达式

    qr!^FILE_DESC_LIMIT=\d+(\s*)!;

但这在Perl 5.16中不起作用,因此我尝试打印此表达式. 在5.10中,我看到

    (?-xism:\bFILE_DESC_LIMIT=\d+(\s*))

在Perl 5.16中,我看到

    (?^:^FILE_DESC_LIMIT=\d+(\s*))

我看到qr在添加-xism时没有在5.16中添加它,为什么这里有区别?

如果我在正则表达式中显式添加它,它将起作用.但是我对解决方案表示怀疑,因为Perl没有明确添加它,这里可能存在一些问题.

在Perl 5.16中应该使用一些其他方法吗? 为什么Perl改变了这种行为?

我的代码如下所示:我们正在创建一个正则表达式,然后使用正则表达式对一个表达式进行赋形,然后将该表达式在打开的文件中求值,然后将求值的输出写入同一文件.

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);

我在perl 5.16中所做的更改是,我将-xism修饰符显式添加到正则表达式中,还修改了\ b修饰符条件

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);

我不知道问题是由于-xism修饰符还是由于\ b修饰符,如我所见,脚本是在5.10版上进行修改的,而\ b修饰符是显式添加的,可能真正的问题出在此修饰符上./p>

参考下面的答案之一,?^等同于?-xism,我删除了代码

if ($] >=5.016)
{
    $tempRegex=~ s!\^!-xism!;
}

从我的脚本开始,-xism不再是我的正则表达式的一部分,并且我在我的正则表达式中添加了\ b,就像对perl版本5.10所做的那样,下面的代码替换了我的正则表达式中的第二个插入符号((?^:^带有\ b的FILE_DESC_LIMIT = \ d +(\ s *)),结果为正则表达式(?^:\ bFILE_DESC_LIMIT = \ d +(\ s *)),该字符串被匹配并替换

if ($] =~ /5\.016/)
{
    my $n=0;
    $Source =~ s/\^/++$n==2?"\\b":"\^"/ge
}

正如我在正则表达式教程中看到的,\ b用于定义单词边界并执行整个单词匹配,我不清楚为什么它不能匹配 文件中的FILE_DESC_LIMIT = 2048字符串不带\ b.

解决方案

关于文档, perlre 中的扩展模式具有此

从Perl 5.14开始,紧接在"?"之后的"^"(脱字符或抑扬音符号)是与d-imnsx等效的简写.标记(除"d"之外)可能会在插入符号后进行覆盖以将其覆盖.但是减号是不合法的.

在这些修饰符中,-imsx匹配您在v5.10中看到的内容,而-n启用括号的捕获行为,而d启用默认"字符集行为.最后两个是新的修饰符

这些都不与您在v5.10中显示的值有任何不同,因此您将必须显示数据以及您写的内容才能改变行为

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 to d-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

这篇关于Perl 5.16中的qr操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆