Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: find the package name of an inherited object from inside the parent

by moritz (Cardinal)
on Jul 07, 2013 at 09:15 UTC ( #1042977=note: print w/ replies, xml ) Need Help??


in reply to find the package name of an inherited object from inside the parent

The Mooseish way to get the dynamic class name is $obj->meta->name:

use 5.010; package Parent; use Moose; sub who_am_I { shift->meta->name }; package Child; use Moose; extends 'Parent'; package main; say Child->new->who_am_I; __END__ Child

$obj->meta returns the metaclass instance for the object's class, and ->name returns the name (surprise, surprise).

(excursion: In Perl 6 it works basically the same, except that there is a shortcut for calling methods on the metaclass: say 1.^name says Int).


Comment on Re: find the package name of an inherited object from inside the parent
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2015-07-30 23:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls