in reply to Defining a subroutine in another package

I think there's a way without circumventing strict, however I only have a vague recollection of something I read in the Camel book about this, way back when. I revolves around the %main:: hash.. I'm not sure how one would go about it though. I spent a while examining the output of perl -MData::Dumper -e'print Dumper(\%main::);' but can't seem to reach a useful conclusion. What I was looking for is a way to go through a hash lookup via $main::{$package}. Anyone up to fill the void?

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: Defining a subroutine in another package
by chromatic (Archbishop) on Jan 27, 2002 at 03:10 UTC
    Thank you for jogging my memory:
    use strict; my $sub = sub { print "Hello!\n"; }; my $var = 'Bar'; my $pak = 'Foo::'; my $ref = \%main::; $ref->{'Foo::'}{bar} = $sub; $main::{'Foo::'}{$var} = $sub; $ref->{$pak}{baz} = $sub; $main::{$pak}{boo} = $sub; Foo::bar(); Foo::Bar(); Foo::baz(); Foo::boo()
    Update: added variable package name examples
      Ah so I can use them that way. Data::Dumper produces entries like 'UNIVERSAL::' => *{'::UNIVERSAL::'} for keys denoting a package name, which confused me - can you explain what that is? Is that a symbolic typeglob reference, or something completely different? (Incidentally, typeglobs are the only part of Perl I never even came close to grokking. I understand what they are supposed to be, but the concept is still very alien.)

      Makeshifts last the longest.

        I think it's just there to look pretty. The leading package separator means it's in package main. The trailing package separator gets at the package stash. The typeglob dereference does point it at the typeglob. I'm not sure I'd call it symbolic, as a typeglob is sort of a special kind of hash...

        All typeglobs do is associate the seven things that can be named with global names. Think of them as hashes that can only hold six keys -- SCALAR, ARRAY, HASH, CODE, IO, FORMAT. These are called slots, and you can get at the content of a glob by using the slot name as you would a hash key. The global (non-lexical) $foo, @foo, %foo, *foo, and &foo can all live within the typeglob *foo. Internally, anything that tries to use something with the name 'foo' will be resolved to use one of those things. Perl grabs the appropriate glob (resolving package names as necessary), then grabs the requested item from the appropriate glob slot.

        Again, these are all things that have global and not lexical names.

        You can copy typeglobs and assign to them. If you assign it a reference to something, it'll automatically populate (or overwrite) the appropriate slot. That's how Exporter works:

        *{$package . '::mynewsub'} = \&mynewsub;
      Is there a benefit to using this method over assigning the subref to a glob?

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        If you know the name of the glob at compile-time, no. However, note that the code performs a glob assignment with names from scalars without disabling strict 'refs'.