Perl 脚本适用于 5.8.4 和 5.12.4,但不适用于 5.10.0
我有来自用户的以下脚本。当在 perl 5.8.4 和 5.12.4 上运行时,它会打印 1,但在 5.10.0 中则不会(打印 0)。
perl -e ' my $a=0; my $net="<*2>ColBnk<2"; if (($net =~ s/([(<])/$1/g) != ($net =~ s/([>)])/$1/g)){$a++} print $a;'
这是代码的问题吗(将字符串变量与 !=
而不是 ne
进行比较,或者可能是由于 $a++
之后缺少分号) 。 我尝试通过打印 ($net =~ s/([(<])/$1/g
) 和 ($net =~ s/([> ;)])/$1/g
) 并且两者都打印相同的值。请参考下面的代码,
use strict;
use warnings;
#/mu/bin/perl -e '
my $a=0;
my $net="<*2>ColBnk<2";
my $test="<*2>ColBnk<2";
$test =~ s/([(<])/$1/g;
print "\n$test" ;
$test =~ s/([>)])/$1/g;
print "\n$test" ;
if (($net =~ s/([(<])/$1/g) ne ($net =~ s/([>)])/$1/g)){$a++;}
print "\n Value of a =$a";
#'
请您提供任何指示。 感谢您的帮助, 阿什什
I have the below script from a user. When ran on perl 5.8.4 and 5.12.4, it prints 1, but not in 5.10.0 (prints 0).
perl -e ' my $a=0; my $net="<*2>ColBnk<2"; if (($net =~ s/([(<])/$1/g) != ($net =~ s/([>)])/$1/g)){$a++} print $a;'
Is this the problem with the code (comparing string variable with !=
instead of ne
or could this be due to missing semicolon after $a++
).
I tried to debug it by printing the value of ($net =~ s/([(<])/$1/g
) and ($net =~ s/([>)])/$1/g
) and both are printing the same value. Please refer the below code
use strict;
use warnings;
#/mu/bin/perl -e '
my $a=0;
my $net="<*2>ColBnk<2";
my $test="<*2>ColBnk<2";
$test =~ s/([(<])/$1/g;
print "\n$test" ;
$test =~ s/([>)])/$1/g;
print "\n$test" ;
if (($net =~ s/([(<])/$1/g) ne ($net =~ s/([>)])/$1/g)){$a++;}
print "\n Value of a =$a";
#'
Could you please provide any pointers.
Thanks for your help,
Ashish
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以确认它在 5.8.9 和 5.14.2 中工作,但在 5.10.0 中不起作用。这肯定是 5.8 和 5.10 之间引入的错误,随后已修复。我使用 Porting/bisect.pl 来找出它的问题所在。这是 Perl 5.9.4 中引入的此提交。然后它被 Perl 5.10.1 中引入的此提交修复。
当我尝试使用 bisect.pl 时,我不断收到有关需要 C 编译器的错误(即使我确实安装了一个)。事实证明,bisect.pl 没有使用 Perl 的配置脚本的正常编译器检测(大概是为了节省时间)。如果默认不起作用,则需要使用 bisect.pl 的
-D
选项进行设置。 (例如,我使用 -D{cc,ld}=/usr/bin/gcc)。您可以通过执行以下步骤找到引入该错误的确切更改:
如果您必须使用 Perl 5.10.0,则可以通过存储每个
s///g
的结果来解决该错误成一个临时变量,然后比较两个变量:但更好的解决方案是升级到固定版本。 Perl 5.10 已正式终止生命周期,但如果您不想进行较大更改,您可以安装 5.10.1。或者您可以升级到 5.12 或 5.14。
I can confirm that it works in 5.8.9 and 5.14.2, but does not work in 5.10.0. It must be a bug introduced somewhere between 5.8 and 5.10, and subsequently fixed. I used Porting/bisect.pl to find out where it broke. It was this commit, which was introduced in Perl 5.9.4. It was then fixed by this commit, which was introduced in Perl 5.10.1.
When I tried using bisect.pl, I kept getting an error about needing to have a C compiler (even though I do have one installed). It turns out that bisect.pl doesn't use the normal compiler detection of Perl's Configure script (presumably to save time). If the default doesn't work, you need to use the
-D
option of bisect.pl to set it. (e.g., I used-D{cc,ld}=/usr/bin/gcc
).You can find the exact change that introduced the bug by performing the following steps:
If you must use Perl 5.10.0, then you can work around the bug by storing the result of each
s///g
into a temporary variable, and then comparing the two variables:But a better solution is to upgrade to a fixed version. Perl 5.10 is officially end-of-life, but you could install 5.10.1 anyway if you don't want to make a large change. Or you can upgrade to 5.12 or 5.14.