为什么在这种情况下Perl可以自动生存?

编程入门 行业动态 更新时间:2024-10-28 12:31:16
本文介绍了为什么在这种情况下Perl可以自动生存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为什么$a成为arrayref?我什么都没推.

Why does $a become an arrayref? I'm not pushing anything to it.

perl -MData::Dumper -e 'use strict; 1 for @$a; print Dumper $a' $VAR1 = [];

推荐答案

这是因为for循环将@$a的内容视为左值,您可以将其赋值.请记住,for将数组的内容别名为$_.看起来即使在没有别名的内容的情况下,在@$a中查找可别名内容的行为也足以引起自动生存.

It is because the for loop treats contents of @$a as lvalues--something that you can assign to. Remember that for aliases the contents of the array to $_. It appears that the act of looking for aliasable contents in @$a, is sufficient to cause autovivification, even when there are no contents to alias.

这种混叠的效果也是一致的.以下还会导致自动生存:

This effect of aliasing is consistent, too. The following also lead to autovivification:

  • map {stuff} @$a;
  • grep {stuff} @$a;
  • a_subroutine( @$a);
  • map {stuff} @$a;
  • grep {stuff} @$a;
  • a_subroutine( @$a);

如果要管理自动生存,可以使用同义的编译指示来实现词法控件

If you want to manage autovivification, you can use the eponymous pragma to effect lexical controls.

更多推荐

为什么在这种情况下Perl可以自动生存?

本文发布于:2023-08-04 02:24:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1292020.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在这种情况下   可以自动   Perl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!