在我的Perl代码我用下面一行:虽然我不使用如何避免额外的信息被打印
Use of uninitialized value $host in string eq at cli.pl line 18.
:
$host=$hostname if ($host eq undef);
在我收到以下消息输出strict
。我怎样才能避免这条消息被输出打印?
在我的Perl代码我用下面一行:虽然我不使用如何避免额外的信息被打印
Use of uninitialized value $host in string eq at cli.pl line 18.
:
$host=$hostname if ($host eq undef);
在我收到以下消息输出strict
。我怎样才能避免这条消息被输出打印?
使用defined
功能:
$host=$hostname if (!defined $host);
或者更短的形式:(而use strict
,真的)
$host=$hostname unless $host;
此操作可能会有帮助:defined-or operator
$host = $host // $hostname;
如果$host
定义甚至它的价值是''
,0
,'0'
,它将使用$host
作为其值, 只有$host
是undef
,$host
将得到$hostname
的价值
我想你想
$host //= $hostname;
//是“定义的”或“运算符”。 // =是其分配版本。
Thanks Mat ...它解决了这个问题。 – Joe
如果它确实对你有帮助,请考虑阅读以下内容:[接受答案如何工作?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。它会帮助其他人知道这个问题已经解决了。 – Mat