Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Changing the Class Name of a Moose Instance

by tobyink (Abbot)
on Oct 03, 2012 at 16:45 UTC ( #997097=note: print w/replies, xml ) Need Help??


in reply to Changing the Class Name of a Moose Instance

use v5.14; use Moose (); sub with_traits_and_fancy_schmancy_name { my ($name, $base, @roles) = @_; return $base unless @roles; return 'Moose::Meta::Class'->create( $name, superclasses => [$base], roles => \@roles, )->name; } package Person { use Moose; has name => (is => 'ro', isa => 'Str'); sub add_numbers { my $self = shift; my $sum; $sum += $_ for @_; return $sum; } }; package Inumerate { use Moose::Role; around add_numbers => sub { confess "I'm no good at maths"; }; }; my $bob = with_traits_and_fancy_schmancy_name( 'Idiot', 'Person' => qw( Inumerate ), )->new(name => 'Robert'); $bob->add_numbers(1, 2, 3);

Update: previously I didn't include a very good example usage.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^2: Changing the Class Name of a Moose Instance
by jandrew (Chaplain) on Oct 03, 2012 at 17:02 UTC

    Thanks++, that works.

    Update: I just ran the updated example and I like the 'confess' output that includes Idiot::add_numbers('Idiot=HASH(0x337cf38)', 1, 2, 3). Nice touch

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997097]
help
Chatterbox?
[Corion]: At least back in the day Firefox (well, Netscape) used to cache the DNS itself because there was no OS API to asynchronously resolve names (?) Maybe that code still lives on
[Corion]: Mhmm. Our datawarehouse is named after the department, except that the department changed its name one year ago. Now I guess I'm looking for the moral equivalent of "THE Datawarehouse", where "THE" stands for "Traditional High-valued Expertise" ;)

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2017-07-26 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (386 votes). Check out past polls.