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

Re: Why does Perl have typeglobs?

by ikegami (Pope)
on Jul 08, 2014 at 21:10 UTC ( #1092798=note: print w/replies, xml ) Need Help??


in reply to Why does Perl have typeglobs?

Type globs are symbol table entries. Perl has a type globs because Perl has a symbol table at runtime. Perl has a symbol table at runtime because it allows code to be compiled after runtime has started.

For example, the following adds bar to the symbol table after Mod.pm has been executed.

use Mod; sub bar { ... } ...

Perl doesn't have to expose the type globs to the program, but it makes some incredible things possible. A simple example is aliasing a sub.

sub foo { ... } *Foo = \&foo; # Create alias for backwards compatibility.

Replies are listed 'Best First'.
Re^2: Why does Perl have typeglobs?
by Anonymous Monk on Jul 08, 2014 at 21:21 UTC
    Yes, but my question wasn't why Perl have symbol table entries. It was why they are structs with a bunch of pointers to different things: arrays, scalars etc... rather then one pointer to one thing per struct. I came to the conclusion that it was some attempt to gain effeciency... assuming that programmers would actually use that feature (reusing variable names; for ex. $foo, @foo, %foo etc). I don't think that too many people actually do that, am I wrong?

      It was why they are structs with a bunch of pointers to different things

      Perl can have arrays, scalars, hashes, etc with the same name. Therefore, the symbol table entry for a name must be able to hold all of them.

      I don't think that too many people actually do that, am I wrong?

      It doesn't matter how many do. It just matters if they can. That said, virtually every program uses both $_ and @_. The numerous programs using <> use $ARGV, @ARGV and *ARGV{IO}.

        I thought about it some more, and it appears that efficiency can't be the reason. I don't see why symbol tables even need to have any structs (as opposed to just bare pointers). Judging by the header you provided, the glob doesn't actually do much of anything, other that being a collection of pointers. So it was "Larry's idea" then, I guess :)

      You can e.g. assign a typeglob to another:

      $foo = 42; @foo = ( 1, 2, 3 ); *bar = *foo; print "$bar @bar"; undef *bar; print "$bar @bar";
      Where and why that might come useful I cannot tell; however, it does make sense to keep a single struct, does it not?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2019-08-24 00:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?