Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How to programmatically determine package where a sub was called

by einhverfr (Monk)
on Nov 06, 2013 at 06:22 UTC ( #1061399=perlquestion: print w/ replies, xml ) Need Help??
einhverfr has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to add fully declarative mappings between database functions and classes (I am also using Moo for declarative definition of properties). I would like to create a function has_dbmethod() which would have a similar syntax to has(), but would automatically generate wrapper functions for database functions. Now obviously I can't use __PACKAGE__ because I need it to be in the subclass's package, not the parent. So how can I detect the namespace of the subclass, at runtime, which calls this function?

Comment on How to programmatically determine package where a sub was called
Re: How to programmatically determine package where a sub was called
by kcott (Abbot) on Nov 06, 2013 at 06:42 UTC

    G'day einhverfr,

    "So how can I detect the namespace of the subclass, at runtime, which calls this function?"

    It sounds like caller will do what you want. There's three ways to use this function, each returns the caller's package name as the first argument:

    $package = caller; ($package, $filename, $line) = caller; ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash) = caller($i);

    See the doco for full details.

    -- Ken

Re: How to programmatically determine package where a sub was called
by tobyink (Abbot) on Nov 06, 2013 at 23:37 UTC

    kcott mentions caller which does indeed do what you want. However, it's worth mentioning that Moo doesn't use that technique.

    Instead, when you import the has keyword into your class, Moo builds a custom has function just for you, which always adds attributes to your class, no matter where it's called from. Demonstration:

    use v5.12; { package Foo; use Moo; { package Goo; # The caller here is "Goo". Foo::has( xxx => (is => 'ro') ); } } # But the attribute was created in "Foo"! say Foo->can('xxx');

    This is not as difficult as it sounds.

    Moose does more or less the same thing. Mouse doesn't; Mouse looks at caller.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2014-08-21 17:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (138 votes), past polls