Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I have been searching unsuccessfully for a way to change the class name of a Moose instance. Most specifically when I am composing Moose classes on the fly using Moose::Util 'with_traits'. Any helpful suggestions are appreciated.

The initial use case for me is to make the confess output from Carp more readable. I have included demonstration code below.

package MyClass; use Moose; has 'name' => ( is => 'ro', writer => 'set_name', predicate => 'has_name', ); no Moose; __PACKAGE__->meta->make_immutable; package Respect; use Moose::Role; use Carp qw( cluck ); sub get_respectful_name{ my ( $self, ) = @_; if( $self->has_name ){ return $self->name . " - San\n"; }else{ cluck "No name is available"; return undef } } no Moose::Role; use Moose::Util qw( with_traits ); use Smart::Comments '###'; my $business_associate = with_traits( 'MyClass', ( 'Respect', ), )->new(); print "Hello " . $business_associate->get_respectful_name . "\n"; $business_associate->set_name( 'Miyagi' ); print "Hello " . $business_associate->get_respectful_name . "\n"; 1;

The output is as follows

No name is available at MooseClassName.pl line 22 Respect::get_respectful_name Moose::Meta::Class::__ANON__::SERIAL::1= HASH(0x3591540)') called at MooseClassName.pl line 39 Use of uninitialized value in concatenation (.) or string at MooseClassName.pl line 39. Hello Hello Miyagi - San

The goal would be to set -Moose::Meta::Class::__ANON__::SERIAL::1- to something more human readable.

It seems there might be other use cases for this but as a minimum the 'meta' capabilities of Moose have spoiled me into believing that all information about an instance is accessible and mutable as needed. I just can't seem to figure out how.

Update: spelling corrections


In reply to Changing the Class Name of a Moose Instance by jandrew

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (14)
    As of 2014-08-28 15:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (263 votes), past polls