<?xml version="1.0" encoding="windows-1252"?>
<node id="997078" title="Changing the Class Name of a Moose Instance" created="2012-10-03 11:37:42" updated="2012-10-03 11:37:42">
<type id="115">
perlquestion</type>
<author id="706986">
jandrew</author>
<data>
<field name="doctext">
&lt;p&gt;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 [https://metacpan.org/module/Moose::Util|Moose::Util] 'with_traits'. Any helpful suggestions are appreciated.&lt;/p&gt;
&lt;p&gt;The initial use case for me is to make the confess output from Carp more readable. I have included demonstration code below.&lt;/p&gt;
&lt;readmore&gt;
&lt;c&gt;
package MyClass;
use Moose;

has 'name' =&gt; (
	is =&gt; 'ro',
	writer =&gt; 'set_name',
	predicate =&gt; 'has_name',
);

no Moose;
__PACKAGE__-&gt;meta-&gt;make_immutable;

package Respect;
use Moose::Role;
use Carp qw( cluck );

sub get_respectful_name{
	my ( $self, ) = @_;
	if( $self-&gt;has_name ){
		return $self-&gt;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',
					),
				)-&gt;new();

print "Hello " . $business_associate-&gt;get_respectful_name . "\n";
$business_associate-&gt;set_name( 'Miyagi' );
print "Hello " . $business_associate-&gt;get_respectful_name . "\n";
1;
&lt;/c&gt;
&lt;p&gt;The output is as follows&lt;p&gt;
&lt;c&gt;
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
&lt;/c&gt;
&lt;p&gt;The goal would be to set -Moose::Meta::Class::__ANON__::SERIAL::1- to something more human readable.&lt;/p&gt;
&lt;/readmore&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; spelling corrections&lt;/p&gt;</field>
</data>
</node>
