如何通过typeglob分别访问文件句柄和格式?

时间:2011-07-21 13:41:14

标签: perl typeglob

enter image description here

似乎filehandleformat没有任何前缀,因此我无法通过%{*spud}这样的技巧来引用它们。

我有什么遗漏吗?

更新

如何访问format?为什么$fd=*STDOUT$fd=\*STDOUT都有效?

UPDATE2

代码:

package Foo;
our $spud = 'a scalar';
our @spud = 'an array';
our %spud = (a => 'hash');
sub spud {}
format spud =
.
open 'spud', $0; 

my $stash = \%Foo::;
my $name  = 'spud';
my $glob  = $$stash{$name};

for my $type (qw(SCALAR ARRAY HASH CODE IO FORMAT)) {
    #this works
    #print *$glob{$type}, $/; 
    #this doesn't work,only output one row of scalar                                  
    print *{$FOO::{spud}}{$type}, $/; 
}

输出:

[root@perl]# perl tb
SCALAR(0xa4dcf30)





[root@perl]# 

2 个答案:

答案 0 :(得分:2)

{package Foo;
    our $spud = 'a scalar';
    our @spud = 'an array';
    our %spud = (a => 'hash');
    sub spud {}
    format spud =
.
    open 'spud', $0;
}

my $stash = \%Foo::;
my $name  = 'spud';
my $glob  = $$stash{$name};

for my $type (qw(SCALAR ARRAY HASH CODE IO FORMAT)) {
     print *$glob{$type}, $/;
}

打印:

SCALAR(0x810070)
ARRAY(0x81c750)
HASH(0x81bb00)
CODE(0x81bbd0)
IO::Handle=IO(0x81b670)
FORMAT(0x81bc40)

答案 1 :(得分:0)

* spud将为您提供glob类型的文件句柄。见这里:Typeglobs and Filehandles