Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
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 chilling in the Monastery: (5)
As of 2014-07-26 14:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (177 votes), past polls