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

typeglobs可以在Perl中用作引用的原因

发布时间:2020-12-15 23:35:07 所属栏目:大数据 来源:网络整理
导读:对不起,可能是不明确的问题,但我问它,因为我不喜欢阅读smth,不明白我在读什么. 以下是“Programming Perl”的片段: Since the way in which you dereference something always indicates what sort of referent you’re looking for ,a typeglob can be use
对不起,可能是不明确的问题,但我问它,因为我不喜欢阅读smth,不明白我在读什么.

以下是“Programming Perl”的片段:

Since the way in which you dereference something always indicates what sort of
referent you’re looking for
,a typeglob can be used the same way a reference can,
despite the fact that a typeglob contains multiple referents of various types. So
${*main::foo} and ${$main::foo} both access the same scalar variable,although
the latter is more efficient.

对我来说,这似乎是一种错误,如果是这样的话,那就是正确的:

you can use a typeglob instead of the scalar variable because reference is always a scalar and compiler knows what you need.

本书的文本阅读器可以假设引用可以是标量变量之外的其他内容(即符号表中的标量条目).一旦我看到一个警告:不推荐使用数组作为引用,所以在我看来很久以前“Programming Perl”中的这一段是有意义的,因为引用可能不仅仅是一个标量,而是在新的第4版它根本没有改变,以符合现代Perl.

我检查了本书的勘误页面,但一无所获.

我的假设是否正确?如果不是,那么有人会如此愉快地解释,我错了.

先感谢您.

解决方法

不.它的含义是,与普通参考不同,typeglob同时包含多种类型的东西.但是你取消引用它的方式表明你想要哪种类型的东西:

use strict;
use warnings;
use 5.010;

our $foo = 'scalar';
our @foo = qw(array of strings);
our %foo = (key => 'value');

say ${ *foo };                  # prints "scalar"
say ${ *foo }[0];               # prints "array"
say ${ *foo }{key};             # prints "value"

您不需要特殊的“typeglob解除引用语法”,因为正常
解除引用语法已经指示要取消引用的typeglob的哪个插槽.

请注意,这不适用于我的变量,因为词法变量与typeglobs无关.

旁注:“数组作为参考”警告与此无关.它引用了这种语法:@ array-> [0](意思与$array [0]相同).这从来没有打算成为有效的语法;它偶然滑入了Perl 5解析器,并且在Larry注意到之后被弃用了.

(编辑:李大同)

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

    推荐文章
      热点阅读