Learning Typeglobs, Symbol Tables and the More Obscure Features?

  • Thread starter Veli-Pekka Tätilä
  • Start date
V

Veli-Pekka Tätilä

Hi,
I'd like to learn more about the various ways to access and use type globs
and how to deal with symbol table entries. Having started out with Perl 5.8,
I try to generaly stick to modern solutions such as references, exporter and
lexical file handles. Yet both type globs and symbol tables seem to pop up
in interesting places and appear somewhat related because symbol tables hold
globs and even some of the glob syntax looks like dereferencing to me. As
for usage, aliasing variable names like English does, creating truely
"weird" classses based on type globs, implementing callbacks using the
caller's package variables such as those in List::Util and installing new
subroutines (Memoize) or applying reflection come to mind.

Most books only briefly mention type globs and symbol tables, and even
Programming Perl has the information scattered in various places. Advanced
Perl Programming appears to have a whole chapter on the topic, number 3 that
is, but even so not all of the syntax such as *foo{thing} is covered, as far
as I can tell.

Frankly speaking I'm not happy with Perldoc's approach either. I've done
some digging and found bits and pieces here and there such as in perlmod,
perldata, perlref and perlsub. There's partial overlap in those docs and I
would describe the tone as: oh yeah, there are type globs, but I'm not sure
if you'll ever need them, and even if you do, we'll just quickly mention
them here to get to the more important stuff.

So are there any good books, tutorials or references that would fully cover
symbol tables and type globs, in particular, their usage in Perl 5? I'd
prefer on-line sources. Now that I'm here I'll also slip in another
question, howabout docs on the rest of the more obscure features that make
Perl perl such as formats and symbolic references under use strict 'vars'.
While not often needed, I'd like to read up more on those, too, and have a
feeling that I might have to maintain someone else's ancient Perl code some
day. Learning Perl doesn't cover formats any more, and most books deal with
symbolic references vaguely if at all.

PS: Is it a type glob or a typeglob or are both forms OK?
 
A

anno4000

Veli-Pekka Tätilä said:
Hi,
I'd like to learn more about the various ways to access and use type globs
and how to deal with symbol table entries.

Don't sweat it. There is one thing a Perl programmer needs to know
about globs, and that is what

*glob = $ref;

does, where $ref is an arbitrary (non-symbolic) reference. You know
that, don't you? All everyday aliasing and symbol-table munging can
be done through that.

Everything else about globs and symbol tables is best looked up when
you need it and forgotten as soon as possible.
Now that I'm here I'll also slip in another
question, howabout docs on the rest of the more obscure features that make
Perl perl such as formats and symbolic references under use strict 'vars'.

In my opinion, formats are strictly optional. Some people who happen
to know how to work with formats still use them, but the majority of
Perl programmers don't. There is nothing you can do with formats that
can't be achieved with normal IO and one or two modules like Text::Wrap,
and you don't have to concern yourself with yet another sub-language.

Symbolic references are syntactically no different from real refs. Just
use a string that contains the name of a package variable instead of
a real ref, and you'll access that. It's the semantics of symrefs that
makes them messy, since it blurs the distinction between data and
code.
PS: Is it a type glob or a typeglob or are both forms OK?

From one second-language speaker of English to another: both are okay.

Anno
 
V

Veli-Pekka Tätilä

Don't sweat it. There is one thing a Perl programmer needs to know
about globs, and that is what

*glob = $ref;

does, where $ref is an arbitrary (non-symbolic) reference. You know
that, don't you? All everyday aliasing and symbol-table munging can
be done through that.
OK, thanks for the tip. In light of this, it is hardly Surprising that type
globs seem to get only passing mentions in the docs, then. While I do
realize that Perl can never be mastered entirely, which for me is a reason
to like the language, I sort of thought I should know about the more exotic
bits.

I just recently realized that when you say:

open my $handle, $mode, $file

the thing stored in the handle is a whole type glob. Print and other
functions then magically use the file handle portion of that glob. So my
question is, what else can be stored in type globs? Subroutines, scalars,
arrays and hashes, of course, plus formats, I think. But are directory
handles yet another kind of "scalar type"? I haven't found the answer in the
docs sofar.

On a side note, maybe it is just me but I find the syntax:

*glob = $ref;

slightly confusing. Plainly put, when I assign to something I tend to think
that the value goes on the left (i.e. the right side is evaluated and
whatever it returns is copied). However, in a type glob assignment the right
side becomes an alias of the left. ANd you don't generaly use the left side
after that, because the right side will do just as well being an alias.
In my opinion, formats are strictly optional. Some people who happen
to know how to work with formats still use them, but the majority of
Perl programmers don't
Which reminds me, are there any recommended Perl libraries for replacing
formats with something more modern and lexical-friendly. One pretty nice
I've come across is Text::Template. The thing that makes it so simple is
that there's virtually no separate sub-language, you can embed PErl code to
be evaluated directly.

From one second-language speaker of English to another: both are okay.
Good. I thought type glob would be preferrable having read that English
contains much less compound words than Finnish does. But then again, the
perldocs ues just that and programmres tend to like $hashref better than
$hash_ref.
 
V

Veli-Pekka Tätilä

Veli-Pekka Tätilä wrote:
On a side note, maybe it is just me but I find the syntax:

*glob = $ref;

slightly confusing. <snip in a type glob assignment
the right side becomes an alias of the left.
Darn, silly me. The assignment goes just the other way around, depending on
how you look at it, so <funny-character>glob would be an alias of
<funny-character>$ref only, after the assignment. Aliasness is symmettric
and the way it is stated in perldata confused me:

quote:
local *Here::blue = \$There::green;

temporarily makes $Here::blue an alias for $There::green, but doesn't make
@Here::blue an alias for @There::green, or %Here::blue an alias for
%There::green
End quote.

My bad as usual, oh well.
Feel free to ignore my syntax complaint earlier in this thread.
 
A

anno4000

Veli-Pekka Tätilä said:
Veli-Pekka Tätilä <[email protected]> wrote:
[typeglobs]

I just recently realized that when you say:

open my $handle, $mode, $file

the thing stored in the handle is a whole type glob. Print and other
functions then magically use the file handle portion of that glob. So my
question is, what else can be stored in type globs? Subroutines, scalars,
arrays and hashes, of course, plus formats, I think. But are directory
handles yet another kind of "scalar type"? I haven't found the answer in the
docs sofar.

No, dir handles appear as a special case of file handle, both are
accessed through (say) *main::HANDLE{ IO} as the case may be.
On a side note, maybe it is just me but I find the syntax:

*glob = $ref;

slightly confusing. Plainly put, when I assign to something I tend to think

In a follow-up you said:
Feel free to ignore my syntax complaint earlier in this thread.

so that's what I'm doing.
Good. I thought type glob would be preferrable having read that English
contains much less compound words than Finnish does. But then again, the
perldocs ues just that and programmres tend to like $hashref better than
$hash_ref.

Heh, the gluing-together of words is one trait shared by our otherwise
widely unrelated languages, Finnish and German. It happens in English
too, but mostly in fixed combinations (household, waterproof), but not
freely productive. In my entirely unqualified opinion, there is a
tendency in English (rather recent, as languages go), to make more
productive use of agglutination.

Off topic? What's that?

Anno
 
V

Veli-Pekka Tätilä

ano 4000 rayden crc 2 per linda
[typeglobs]
open my $handle, $mode, $file
the thing stored in the handle is a whole type glob. Print and other
functions then magically use the file handle portion of that glob.
are directory handles yet another kind of "scalar type"?
No, dir handles appear as a special case of file handle, both are
accessed through (say) *main::HANDLE{ IO} as the case may be.
Umm interesting. Is this dirs are files in disguise part of Perl's Unix
heritage? I found it surprising as a user but some Unix man pages includedd
passages like "if a file is a directory" which let me conclude, ok I suppose
that's the case internally.

[Perl naming]
having read Larry Wall's essay on Perl's natural language principles at:

http://www.wall.org/~larry/natural.html

I wonder if Perl 6 will borrow even more from natural languages.

All of my Perl variables and comments are in English so far, though, so I
don't get to use all the fancy suffixes mentioned later on, <grin>. However,
I've tried using a kind of speech-synth friendly Hungarian notation to
denote the perl datatype in some way. I use singular words for scalars if
they aren't references to collections, plurals for arrays and the number 2
(read as the preposition to) to emphasize the kinds of mappings most hashes
are. Sometimes this can get very awkward and I scrap it after all. Hmm,
awkword would that be towards AWK as opposed to say Perlward <smile>?

Finally regarding compounds, I know programmers speaking English as a second
language who like to separate even the words in compounds, preferring to
write $postCard so the naming practices are absolutely clear to everyone.

[natural language OT]
Heh, the gluing-together of words is one trait shared by our otherwise
widely unrelated languages , Finnish and German. It happens in English
Quite right, things like video card are compounds here but not in English.
In my entirely unqualified opinion, there is a
tendency in English (rather recent, as languages go), to make more
productive use of agglutination.
Off topic? What's that?
I think I've seen the term somewhere. Let me guess glueing together pieces
to words to give them new and well-defined meanings. Finnish uses a number
of such suffixes. One of them is sto denoting a collection of things.
"Kirja" (book) "kirjasto" (library), "laiva" (ship) "laivasto"
(both fleat and the Navy), "kone" (machine) "koneisto" (machinery). Needless
to say not all words can be treated like this, even if it would make sense.
 
C

Charles DeRykus

Veli-Pekka Tätilä said:
Hi,
I'd like to learn more about the various ways to access and use type globs
and how to deal with symbol table entries. Having started out with Perl 5.8,
I try to generaly stick to modern solutions such as references, exporter and
lexical file handles. Yet both type globs and symbol tables seem to pop up
in interesting places and appear somewhat related because symbol tables hold
globs and even some of the glob syntax looks like dereferencing to me. As
for usage, aliasing variable names like English does, creating truely
"weird" classses based on type globs, implementing callbacks using the
caller's package variables such as those in List::Util and installing new
subroutines (Memoize) or applying reflection come to mind.

Most books only briefly mention type globs and symbol tables, and even
Programming Perl has the information scattered in various places. Advanced
Perl Programming appears to have a whole chapter on the topic, number 3 that
is, but even so not all of the syntax such as *foo{thing} is covered, as far
as I can tell.

...

So are there any good books, tutorials or references that would fully cover
symbol tables and type globs, in particular, their usage in Perl 5? I'd
prefer on-line sources. Now that I'm here I'll also slip in another
question, howabout docs on the rest of the more obscure features that make
Perl perl such as formats and symbolic references under use strict 'vars'.
While not often needed, I'd like to read up more on those, too, and have a
feeling that I might have to maintain someone else's ancient Perl code some
day. Learning Perl doesn't cover formats any more, and most books deal with
symbolic references vaguely if at all.
I think the 1st ed. of "Advanced Perl Programming" does a better job
than others. For instance, the section "Glob Values and Symbol Tables"
delves into the internals and explains how both are related. There're
tidbits such as:

Symbol tables are HV's that map variable names to GV's. But aren't
Hv's supposed to store Sv's only? Well, you may have noticed that
all value types have identical-looking wrapper structures that
maintain the reference count, flags, and the pointer to an interior
structure. Because they are identical, you can cast an AV*, HV*, CV*
to an SV* and thus fool the HV into storing anything you want.

I don't recall seeing this level of detail in a single source.

PS: Is it a type glob or a typeglob or are both forms OK?

Advanced Perl Programming uses typeglob. Since it's struct internally,
this agglutinizing somehow seems "righter" and more Perlish than the
loosely coupled "type glob".

Hth,
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
No, dir handles appear as a special case of file handle, both are
accessed through (say) *main::HANDLE{ IO} as the case may be.

This is not so. Filehandles and dirhandles live in different
namespaces. (I have no idea what *HANDLE{IO} does if both exist.)
Witness

perl -wle "open F, shift or die; opendir F, '.' or die; while(1)
{print scalar <F>; print scalar readdir F}" readme*

(assuming readme* exists in the current directory).

Hope this helps (but probably it does not; this is another case of
heavy perl4 heritage),
Ilya
 
A

anno4000

Ilya Zakharevich said:
[A complimentary Cc of this posting was sent to
No, dir handles appear as a special case of file handle, both are
accessed through (say) *main::HANDLE{ IO} as the case may be.

This is not so. Filehandles and dirhandles live in different
namespaces. (I have no idea what *HANDLE{IO} does if both exist.)

I see. So "IO" maps to two distinct slots in a glob. Whoda thunk.

Anno
 
B

Ben Morrow

Quoth (e-mail address removed)-berlin.de:
Ilya Zakharevich said:
[A complimentary Cc of this posting was sent to
arrays and hashes, of course, plus formats, I think. But are directory
handles yet another kind of "scalar type"? I haven't found the answer in the
docs sofar.
No, dir handles appear as a special case of file handle, both are
accessed through (say) *main::HANDLE{ IO} as the case may be.

This is not so. Filehandles and dirhandles live in different
namespaces. (I have no idea what *HANDLE{IO} does if both exist.)

I see. So "IO" maps to two distinct slots in a glob. Whoda thunk.

<ObPedant>

perlref:

| All of these are self-explanatory except for *foo{IO}. It returns the
| IO handle [...] (It still conflates file and directory handles, though.)

:)

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top