[英]Why is 'undef' not detected by this Perl fragment?
I would expect the block in the second 'if' statement to be entered because of the undef value but the logs show that it isn't being entered.由于 undef 值,我希望输入第二个“if”语句中的块,但日志显示它没有被输入。
sub getcmd{
my $self = $_[0];
if ( $self->_recv == OK ){
push @{$self->{'log'}}, ['NOTICE', "OK"];
return "My command";
}
push @{$self->{'log'}}, ['ERROR', "Did not get OK back"];
return undef;
}
...
if (!($ret = $self->getcmd)){
push @{$self->{'log'}}, ['ERROR', "failed to read after asking for NEXT"];
}
else {
push @{$self->{'log'}}, ['ERROR', "'undef' not detected in next()"];
}
The log file shows:日志文件显示:
[Fri May 8 19:25:56 2009]: ERROR: Did not get OK back
[Fri May 8 19:26:02 2009]: ERROR: 'undef' not detected in next()
Any ideas gratefully accepted.任何想法都欣然接受。
Edit: Sorry, I'd edited down the code to show the basic flow.编辑:对不起,我已经编辑了代码以显示基本流程。 I should've proofread it a bit better.我应该把它校对好一点。
Thanks for the suggestions and comments.感谢您的建议和意见。 I hadn't noticed the six second difference in the log timestamps so now I suspect that you're right about the execution sequence being different to what I originally expected.我没有注意到日志时间戳的六秒差异,所以现在我怀疑您对执行顺序与我最初预期的不同是正确的。
I'll go back and look again.我回去再看看。 Guess that's what you get when trying to look at someone else's "average" Perl after a thirteen hour day trying to get things finished for a "must go live on Monday" project!猜猜这就是你在一天 13 个小时试图完成一个“必须在星期一上线”项目之后查看其他人的“平均”Perl 时得到的结果!
I didn't write the code and simply inherited it.我没有编写代码,只是继承了它。 The code was written by a couple of people who think they don't "need no steenking warnings or stricts".该代码是由几个认为他们“不需要警告或严格”的人编写的。
Imagine 800 lines of Perl and lots of 'ifs' but no else statements!想象一下 800 行 Perl 和许多“if”,但没有 else 语句! No defensive coding at all!根本没有防御性编码! 8-O 8-O
Reduced to a bare minimum, this prints "undef detected".减少到最低限度,这将打印“未检测到”。
#!/bin/perl -w
use strict;
sub getcmd
{
return undef;
}
my $ret;
if (!($ret = getcmd()))
{
print "undef detected\n";
}
else
{
print "undef undetected\n";
}
Consequently, your problem is most likely that the $self->getcmd() isn't returning undef, even though you think it should.因此,您的问题很可能是 $self->getcmd() 没有返回 undef,即使您认为它应该返回。
I think something more complicated is going on here -- those log messages seem to be 6 seconds apart, and there's no way it'd take 6 seconds for the push statement, the return, and the if check.我认为这里发生了更复杂的事情——这些日志消息似乎相隔 6 秒,而 push 语句、return 和 if 检查不可能需要 6 秒。
Any chance the first log message was from a previous call of the method from some other place in the application?第一条日志消息是否有可能来自应用程序中其他位置的方法调用?
Use perl debugger (perl -d) to step through the code to see what is going on.使用perl 调试器(perl -d) 单步执行代码以查看发生了什么。 When debugging code, it's important to free your mind from every assumption.在调试代码时,重要的是要让你的头脑从每一个假设中解放出来。
Also, these lines are a must above every perl program:此外,这些行在每个 perl 程序之上都是必须的:
use strict;
use warnings;
The proper way to test $var
for undef
is not if (!$var) ...
, because the test will also be true for $var = ''
and $var = 0
.为undef
测试$var
的正确方法不是if (!$var) ...
,因为对于$var = ''
和$var = 0
的测试也将是正确的。 Use if (!defined $var) ...
instead.使用if (!defined $var) ...
代替。
Maybe like this (showing all relevant cases):也许像这样(显示所有相关案例):
if (!defined $var) {
# not defined ...
} elsif ($var) {
# defined and true...
} else {
# defined and false
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.