Search code examples
perltypeglob

How do I get access to filehandle and format respectively by typeglob?


enter image description here

It seems filehandle and format doesn't have any prefix,so I can't get reference to them by tricks like %{*spud}.

Is there anything I'm missing?

UPDATE

How do I access the format? Why do $fd=*STDOUT and $fd=\*STDOUT both work?

UPDATE2

Code:

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}, $/; 
}

Output:

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





[root@perl]# 

Solution

  • {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}, $/;
    }
    

    prints:

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