princepawn has asked for the wisdom of the Perl Monks concerning the following question:

As I continue my journey thru gnat's stages of a Perl programmer, I reach Slide 32 and have a few questions.

The first thing I think I know. Typeglobs are containers holding references to each of the 7 perl types for a particular variable name. They only exist for package variables. Typeglobs do not exist for lexical variables. Since the only two types of variables in Perl are lexical and package, one can say that 1 of the 2 has typeglobs as its storage support.

Now for some questions. In this slide gnat says:

# Perl lets you use a glob wherever you would use a filehandle, so glo +bs are often used to pass filehandles to a subroutine: open(HANDLE, $filename) or die; mysub(*HANDLE);

That's all well and good. And I assume mysub looks like this:

sub mysub { my $fh = shift; while (<$fh>) { ... } }
But what happens when you want to pass a format or dirhandle to a subroutine instead of a filehandle.

Replies are listed 'Best First'.
Re: typeglob questions
by diotalevi (Canon) on Sep 27, 2002 at 07:04 UTC

    Normally you'd use one of the IO:: objects since now it's 100% nicer to work with. Or at least I'd much prefer to pass around an IO::something object than have to work with typeglobs n' stuff. I gather that this is more of a perl4-ism and justisn't needed anymore (though obviously it still works). The IO::something objects are convenient since at root they're really just typeglob references so the various functions like open/close/read opendir/readdir/closedir will just read the reference and it still works at full speed. Yay!

    For extra credit try this:

    use Devel::Peek; opendir DIR, '.' or die $1; Dump(*DIR); # The IO slot has the dirhandle (I guess)

Re: typeglob questions
by broquaint (Abbot) on Sep 27, 2002 at 09:31 UTC
    But what happens when you want to pass a format or dirhandle to a subroutine instead of a filehandle.
    Although globs are synonymous with filehandles they aren't really the same thing. It's just that perl will DWYM when you provide a reference to a glob with an IO reference in it
    $FH = "a scalar"; @FH = qw/an array/; %FH = (a => 'hash'); open(FH, "somefile.txt") or die("dang - $!"); for(qw/SCALAR ARRAY HASH IO/) { print $glob, "{$_} = ', *{$glob}{$_}, $/; } print while <$glob>; __output__ *main::FH{SCALAR} = SCALAR(0x80fd44c) *main::FH{ARRAY} = ARRAY(0x80fd464) *main::FH{HASH} = HASH(0x80fd4a0) *main::FH{IO} = IO::Handle=IO(0x80fd4dc) foo bar baz quux
    So if you use a glob or a reference to a glob where a filehandle is expected and the glob doesn't contain a valid IO reference then it evaluates to undef e.g
    $main::FH = 'a string'; print "nope" if not defined *FH{IO}; __output__ nope

    HTH

    _________
    broquaint

Re: typeglob questions
by flocto (Pilgrim) on Sep 27, 2002 at 10:26 UTC

    Using typeglobs you can generate a reference to your format or io (dirhandle or filehandle) and then pass these, which very easy:

    $io_ref = *typeglob{IO}; $format_ref = *typeglob{FORMAT};

    Regards,
    -octo

Re: typeglob questions
by shotgunefx (Parson) on Sep 27, 2002 at 09:18 UTC
    Formats and Filehandles don't share the same entry in the glob so if the Filehandle FH is undefined and the format FH is, open wouldn't try and use the format value, though I believe DIR handles and Filehandles do occupy the same space.

    -Lee

    "To be civilized is to deny one's nature."