Jac*_*cob 4 regex perl cgi taint-checking
试图解开Perl中的一些变量,以下代码很有用:
if ($year =~ /^(\d{4})$/) {
$year = $1;
} else {
&invalid("year");
}
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,如果有效,$ 1包含$ year.但是,使用?:运算符时,$ 1在有效时包含"1":
($year =~ /^(\d{4})$/) ? $year = $1 : &invalid("year");
Run Code Online (Sandbox Code Playgroud)
有人看到我可能有过错吗?我很困惑为什么会这样.它只发生在这台机器上.或者说,我已成功使用了?运算符用于返回多年的正确匹配变量.我还没有在任何其他机器上尝试过这段代码.
This is Perl, v5.8.8 built for x86_64-linux-thread-multi
Run Code Online (Sandbox Code Playgroud)
这里要小心优先级.
对比http://codepad.org/vEPnhWfH按预期工作,但没有http://codepad.org/nXVU5CA7.当然,我稍微调整了代码以避免调用invalid,但我怀疑这可能是问题的根源.
在这两种情况下,尽管$1包含"2011",所以也许你应该按照mu的第一条评论中的要求显示其他代码.
UPDATE
我更改了键盘示例以使用调用,&invalid并且错误未显示.
sub invalid {
print("INVALID\n");
}
sub check_with_if {
print("Trying with if-statement\n");
my $year = shift;
if ($year =~ /^(\d{4})$/) {
$year = $1;
} else {
&invalid($year);
}
print("\$1 is $1 and \$year is $year"."\n");
}
sub check_with_conditional {
print("Trying with conditional expression\n");
my $year = shift;
($year =~ /^(\d{4})$/) ? $year = $1 : &invalid($year);
print("\$1 is $1 and \$year is $year"."\n");
}
check_with_if("2011");
check_with_conditional("2011");
Run Code Online (Sandbox Code Playgroud)
输出是
Trying with if-statement
$1 is 2011 and $year is 2011
Trying with conditional expression
$1 is 2011 and $year is 2011
Run Code Online (Sandbox Code Playgroud)
编辑2也适用于Mac上的5.12.3.
我同意Jonathan你可能在5.8.8中发现了一个已修复的错误.如果您非常好奇,可以通过Perl更改,例如:
Damien Conway的Perl最佳实践解释了使用匹配变量$1等的一些缺陷,其中之一就是:
$1则不会变为undef,但它会保留其先前的值(当然,在大多数情况下是undef)我的直觉告诉我你的上下文有问题 - 你的表达式可以在标量上下文中进行评估,从而在有效时返回匹配数.
所以,我会尝试重写代码,如:
($year) = ( $year =~ /^(\d{4})$/) or &invalid("year");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
267 次 |
| 最近记录: |