Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Re: Defining a subroutine in another package

by chromatic (Archbishop)
on Jan 27, 2002 at 03:10 UTC ( [id://141849]=note: print w/replies, xml ) Need Help??


in reply to Re: Defining a subroutine in another package
in thread Defining a subroutine in another package

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

Replies are listed 'Best First'.
Re^3: Defining a subroutine in another package
by Aristotle (Chancellor) on Jan 27, 2002 at 03:33 UTC
    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;
Re^3: Defining a subroutine in another package
by dragonchild (Archbishop) on Jan 06, 2005 at 19:08 UTC
    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'.

        Upon further reflection, I think that having to put "no strict 'refs';" is actually a good thing. Without it, you don't have the HERE BE MONSTERS! warning that "no strict 'refs';" or "no warnings 'redefine';" provides. You're just doing hash manipulations which don't look like they're creating new subroutines in global-land. That's not very self-documenting ...

        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-24 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found