Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Of Symbol Tables and Globs

by John M. Dlugosz (Monsignor)
on Nov 08, 2002 at 20:26 UTC ( [id://211526]=note: print w/replies, xml ) Need Help??


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

Replies are listed 'Best First'.
Re: Re: Of Symbol Tables and Globs
by broquaint (Abbot) on Nov 09, 2002 at 16:02 UTC
    The first should say that the symbol table is created when a symbol in a package is mentioned.
    The symbol table is created by the package declaration, which then sets the default package for variables to be declared into for the rest of the lexical scope.
    Saying it's created by the package declaration just introduces more rules later, but the general rule would be right in all cases.
    How does the package declaration introduce more rules later? I think it keeps things a lot simpler as it is explicitly creating a symbol table, where as referring to variables is implicitly creating a symbol table through auto-vivification.
    Huh?
    Indeed, I was never to happy about that particular passage, I'll either try and clear it up or drop it altogether as it isn't terribly important relevant to the rest of the tutorial.
    It is a hash; you just said so.
    Ah, I did just say so and I shouldn't have because symbol tables are not hashes, they are just accessed like hashes. Will clear that up also.
    I would prefer something like, "...use a similar syntax to accessing a hash".
    Ok, will clear that up as well. I'll try and make it clear throughout the tutorial that symbol tables and globs are not hashes, just a similiar (or in the case of symbol tables, exact) way of being accessed.
    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.
    That is true indeed, but I was trying to illustrate the point that although they may look like hashes they most certainly are not, will also do my best to clarify that section as well.
    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".
    Yes, yes and yes :)

    Many thanks for the input!

    _________
    broquaint

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-29 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found