加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

为什么这个(主要是)空的Perl子程序返回一个空字符串?

发布时间:2020-12-15 22:04:12 所属栏目:大数据 来源:网络整理
导读:这是一个Perl程序: use strict;use warnings;use Data::Dumper;sub f { foreach (()) { }}print Dumper(f()); 输出: $VAR1 = ''; 既然没有从f显式返回任何值,并且在其中没有评估表达式,结果不应该是undef?空字符串从哪里来? 解决方法 Since no value is
这是一个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 be undef?

不. 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 a foreach or a while,the returned value is unspecified.

“未指定”是“我们不会记录确切行为,因为我们可以随时改变它,你不应该依赖它”的缩写.现在,它返回PL_no为LeoNerd explained;在未来的Perl版本中,它可能会返回undef或其他一切.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读