Perl 抱怨正则表达式中的未初始化值
我有一个函数如下:
sub getMinFromParam {
my ($param) = @_;
print "PPPP = $param\n";
my $min;
if ($param =~ /\s*\[(\S+),\s*(\S+)\]\s*/) {
print "IN HERE\n";
$min = $1;
}
elsif ($min =~ /((\w+),)+/) {
my @tmp = split (/\s*,\s*/, $param);
if ($tmp[0] =~ /\[(\w+),\s?(\w+)\]/) {
$min = $1;
}
else {
$min = $tmp[0];
}
}
return ($min);
}
当 $param 是一个字符串时,例如: 120u, 421u, 53, 19, 41u, 53, 我收到错误:在模式匹配 (m//) 中使用未初始化的值在这一行:
if ($param =~ /\s*\[(\S+),\s*(\S+)\]\s*/) {
为什么在定义 $param 时抱怨未初始化的值?
I have a function below:
sub getMinFromParam {
my ($param) = @_;
print "PPPP = $param\n";
my $min;
if ($param =~ /\s*\[(\S+),\s*(\S+)\]\s*/) {
print "IN HERE\n";
$min = $1;
}
elsif ($min =~ /((\w+),)+/) {
my @tmp = split (/\s*,\s*/, $param);
if ($tmp[0] =~ /\[(\w+),\s?(\w+)\]/) {
$min = $1;
}
else {
$min = $tmp[0];
}
}
return ($min);
}
When $param is a string like: 120u, 421u, 53, 19, 41u, 53,
I get the error: Use of uninitialized value in pattern match (m//) at this line:
if ($param =~ /\s*\[(\S+),\s*(\S+)\]\s*/) {
Why is it complaining about uninitialized value when $param is defined?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不是。它抱怨这一行:
至少这里有测试数据
120u, 421u, 53, 19, 41u, 53
这是预期的,因为
$min
仅在以下情况下设置: code>$param 匹配,但elsif
仅在$param
不匹配时运行。该行可能应该是 elsif ($param =~ /((\w+),)+/) {,在这种情况下我不会收到警告。
It's not. It's complaining about this line:
At least here with the test data
120u, 421u, 53, 19, 41u, 53
Which is expected, since
$min
is only set if$param
matches, but theelsif
only runs if$param
doesn't match.That line is probably supposed to be
elsif ($param =~ /((\w+),)+/) {
, in which case I don't get a warning.实际上,
$min
是未定义的,当您使用strict
和warnings
时,它会被报告。这修复了它:It is actually
$min
that is undefined, and it gets reported when you usestrict
andwarnings
. This fixes it: