Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

What are typeglobs (useful for)?

by Lhamo_rin (Friar)
on Jun 25, 2005 at 23:50 UTC ( [id://469971]=perlquestion: print w/replies, xml ) Need Help??

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

I'm having a difficult time understanding typeglobs and when they are used. Can someone point me to a good tutorial or offer me a quick lesson in there use. Thanks.

Edited by Arunbear: Changed title from 'typeglobs', as per Monastery guidelines

Replies are listed 'Best First'.
Re: What are typeglobs (useful for)?
by tlm (Prior) on Jun 26, 2005 at 02:42 UTC

    I have never found the documentation for typeglobs particularly clear myself, even when typeglobs were more important to routine Perl programming than they are now (references are a better way of doing many of things that typeglobs were used for before).

    My favorite introduction to typeglobs is ch. 3 of the venerable Advanced Perl Programming (1st edition, 1997), by Sriram Srinivasan. (The much awaited 2nd edition of this book, by Simon Cozens, is a complete re-write, and will not cover this topic.)

    Other sources worth consulting are the Symbol Tables section of perlmod and the Typeglobs and Filehandles section of perldata. These sources have a better treatment (than APP's) of the remaining uses for typeglobs in Perl 5.

    the lowliest monk

Re: What are typeglobs (useful for)?
by borisz (Canon) on Jun 26, 2005 at 00:15 UTC
    I use them as shortcut/alias from time to time:
    *xyz = \&X::YY::ZZ::function;
    or as a alias for a scalar's:
    my $string = 'Hi There'; local *_ = \$string; s/\s+/ /g; print $string; __OUTPUT__ Hi There
    Boris
Re: What are typeglobs (useful for)?
by chas (Priest) on Jun 26, 2005 at 00:45 UTC
    I made some comments on typeglobs in 434748 (the thread is at 434739.) The O'Reilly book "Programming Perl" (The Camel Book) has info about them, and try "perldoc perldata" also.
    chas
Re: What are typeglobs (useful for)?
by Ido (Hermit) on Jun 26, 2005 at 05:54 UTC
Re: What are typeglobs (useful for)?
by ysth (Canon) on Jun 26, 2005 at 23:37 UTC
    Short answer: a typeglob is a symbol table entry; if you have (non-lexical) $foo, %foo, @foo, &foo, and format foo, they are all contained in the same typeglob *foo (along with a file/dir handle). Because filehandles have no specific sigil, you can only mention them in perl by way of the glob.

    So there are two cases where you'd use a typeglob: as a filehandle, or to muck about with the symbol table.

    The most common case of the former is to use one of the perl provided filehandles *STDIN, *STDOUT, *STDERR, *DATA. (The * is unneeded if passing to a function or builtin having a * in the prototype.) For non-perl provided filehandles, using the newer lexical filehandles (actually just a reference to a typeglob, stored in a regular scalar variable) obviates the need for a typeglob.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://469971]
Approved by davidrw
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-19 04:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found