为什么 ref 不 return 'GLOB'?

Why ref does not return 'GLOB'?

我想更好地理解 typeglobs 并编写小程序:

use Symbol;

open F, '>', \my $var;
t( F );
t( \F );
t( *F );
t( \*F );
sub t {
   my $fh =  shift;
   print ">>$fh<<" . ref( $fh ) ."\n";
}

输出为:

>>F<<
>>SCALAR(0x1e67fc8)<<SCALAR
>>*main::F<<
>>GLOB(0x1e53150)<<GLOB

为什么只在最后一种情况下返回 GLOB

  • t( F ): 字符串[1] 不是对 glob 的引用。
  • t( \F ): 对字符串的引用不是对 glob 的引用。
  • t( *F ): glob 不是对 glob 的引用。
  • t( \*F ): 对 glob 的引用是对 glob 的引用。

  1. «在语法中没有其他解释的单词将被视为带引号的字符串。这些被称为“barewords”。»

没有strict,没有其他含义的标识符被解释为裸词,裸词产生字符串。这解释了前两行(第二行参见 print \"F")。

globs 的插值(第 3 行)记录在 perlref

*foo{NAME} and *foo{PACKAGE} are the exception, in that they return strings, rather than references. These return the package and name of the typeglob itself, rather than one that has been assigned to it. So, after *foo=*Foo::bar, *foo will become "*Foo::bar" when used as a string, but *foo{PACKAGE} and *foo{NAME} will continue to produce "main" and "foo", respectively.