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

前端之家收集整理的这篇文章主要介绍了typeglobs可以在Perl中用作引用的原因前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对不起,可能是不明确的问题,但我问它,因为我不喜欢阅读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注意到之后被弃用了.

猜你在找的Perl相关文章