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

perl – “my $foo = $x if $y”语法有什么问题?

发布时间:2020-12-15 21:42:19 所属栏目:大数据 来源:网络整理
导读:在我的最后一个问题中,@ amon给了 an great answer.但是,他也说过: First of all,please don’t do my $foo = $x if $y . You get unexpected and undefined behavior,so it is best to avoid that syntax. 因为上面的结构我在CPAN中看到了很多来源,我想知
在我的最后一个问题中,@ amon给了 an great answer.但是,他也说过:

First of all,please don’t do my $foo = $x if $y. You get unexpected
and undefined behavior,so it is best to avoid that syntax.

因为上面的结构我在CPAN中看到了很多来源,我想知道如何,何时,哪里可能是错的. (一些示例代码会很好).想知道,为什么perl允许它,如果它是坏的.

解决方法

他的措辞实际上有点松懈.这个措辞实际上是我的.让我们从 documentation开始:(重点是原创)

NOTE: The behaviour of a my,state,or our modified with a statement modifier conditional or loop construct (for example,my $x if ...) is undefined. The value of the my variable may be undef,any previously assigned value,or possibly anything else. Don’t rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.

更确切地说,问题是当我的可能没有被执行时使用词法变量.

考虑:

# Usage:
#   f($x)  # Store a value
#   f()    # Fetch and clear the stored value

sub f {
   my $x if !@_;
   if (@_) {
      $x = $_[0];
   } else {
      return $x;
   }
}

f('abc');
say "<",f(),">"   # abc

这显然不是我记录的行为.

Because the above construction I was see in really many sources in the CPAN

那个代码很麻烦.

(编辑:李大同)

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

    推荐文章
      热点阅读