http://qs321.pair.com?node_id=211526


in reply to Of Symbol Tables and Globs

Perl has two different types of variables - lexical and dynamic

I agree with chromatic, that's not a dynamic variable per-se, that's a global variable. Saving it and restoring it via local is a separate effect and only meaningful when a variable is shared among functions, but that doesn't mean it must be a package global to be "dynamic". Although local doesn't work, a lexical variable at package scope (or in a block surrounding several related functions) could also push/pop the value in a similar manner.

but the most common way in which they are created is through the package declaration...Notice how we didn't use a package declaration there?

That's confusing. The first should say that the symbol table is created when a symbol in a package is mentioned. This doesn't have to be done by using the local package within that package, just refer to a variable anywhere. Saying it's created by the package declaration just introduces more rules later, but the general rule would be right in all cases.

The double colons allow the separation of symbol tables

Huh?

we treat it like we would a hash

It is a hash; you just said so. I think you mean "...would any other hash.".

Ever assign something to a symbol table hash entry that wasn't a glob?

Note that $globtut::{variable} = \$foo; means different things depending on whether there is already an entry by that name or it is added by the assignment. Contrast with ${"*globtut::variable"}= \$foo; which auto-vivifies as a glob.

To access the individual slots just treat the glob like a hash e.g

I would prefer something like, "...use a similar syntax to accessing a hash".

${ *scalar{FOO} } = "the FOO data type"; Your error is not due to any protection on symbol tables. Rather, *scalar{FOO} gives undef (rather than a run-time error; go figure) and then the $ dereference gives the error. It's not auto-vivifying like a hash entry would, but it's not a hash! It's a special hash-like syntax. Interesting that the reading of it is not an error.

Another thing to be noted from the above example is that you can't assign toglob slots directly, only through dereferencing them.

Very interesting... I always thought I could assign a reference to it, just never bothered because you don't have to use the subscript on the left-hand-side since it knows what kind of reference you are assigning. That is, *scalar= \$x instead of *scalar{SCALAR}=\$x

we have succesfully imported Foo's subroutines into the main package and nothing else

1) you imported into globtut package, not main package. 2) the grammar is compound-noun((noun(main package) AND noun(nothing else)), which I don't think is what you meant. A couple typos. missing space, "the the", "callers symbol table", "callers package".

—John