一个类型团*foo持有到的内容引用全球使用该名称的变量:$foo,@foo,$foo,&foo,等你可以像一个哈希访问,并分配给直接操作符号表(恶!)。
use v5.10; # necessary for say our $foo = "foo"; our $bar; say ref *foo{SCALAR}; # SCALAR say ${ *foo{SCALAR} }; # bar *bar = *foo; say $bar; # bar $bar = 'egg'; say $foo; # egg
处理文件时,通常会处理Typeglob。open例如,当被要求创建非全局文件句柄时,生成对typeglob的引用:
use v5.10; # necessary for say open(my $log, '> utf-8', '/tmp/log') or die $!; # open for writing with encoding say $log 'Log opened'; # You can dereference this globref, but it's not very useful. say ref $log; # GLOB say (*{$log}->{IO} //'undef'); #undef close $log or die $!;
Typeglobs也可以用于创建全局只读变量,尽管use constant使用范围更广。
# Global constant creation *TRUE = \('1'); our $TRUE; say $TRUE; # 1 $TRUE = ''; # dies, "Modification of a read-only value attempted" # use constant instead defines a parameterless function, therefore it's not global, # can be used without sigils, can be imported, but does not interpolate easily. use constant (FALSE => 0); say FALSE; # 0 say &FALSE; # 0 say "${\FALSE}"; # 0 (ugh) say *FALSE{CODE}; # CODE(0xMA1DBABE) # Of course, neither is truly constant when you can manipulate the symbol table... *TRUE = \(''); use constant (EVIL => 1); *FALSE = *EVIL;