@H_404_1@这是一个Perl程序:
use strict; use warnings; use Data::Dumper; sub f { foreach (()) { } } print Dumper(f());
输出:
$VAR1 = '';
既然没有从f显式返回任何值,并且在其中没有评估表达式,结果不应该是undef?空字符串从哪里来?
解决方法
Since no value is explicitly returned from
f
,and no expressions are evaluated inside it,shouldn’t the result beundef
?
不. perldoc perlsub
表示返回值未指定:
If no
return
is found and if the last statement is an expression,its value is returned. If the last statement is a loop control structure like aforeach
or awhile
,the returned value is unspecified.
“未指定”是“我们不会记录确切行为,因为我们可以随时改变它,你不应该依赖它”的缩写.现在,它返回PL_no为LeoNerd explained;在未来的Perl版本中,它可能会返回undef或其他一切.