<?xml version="1.0" encoding="windows-1252"?>
<node id="1000131" title="Re: Core module: Attribute::Handlers behaviour" created="2012-10-20 09:58:08" updated="2012-10-20 09:58:08">
<type id="11">
note</type>
<author id="757127">
tobyink</author>
<data>
<field name="doctext">
&lt;p&gt;Perl attributes are typically handled at CHECK time - between compile time (BEGIN) and INIT time, and way before run time. Your &lt;c&gt;eval&lt;/c&gt; happens at run time, so too late to effect attribute processing.&lt;/p&gt;
&lt;p&gt;It is in fact possible to alter when processing happens for particular attributes, but it needs to be one of the compilation phases (BEGIN, CHECK, INIT or END), and not runtime.&lt;/p&gt;
&lt;center&gt;&lt;p&gt;* * *&lt;/p&gt;&lt;/center&gt;
&lt;p&gt;As it happens I've been working on some aspect-oriented programming stuff for Moose that might be just what you need.&lt;/p&gt;

&lt;code&gt;
{
	package User;
	use Moose;
	
	has name =&gt; (is =&gt; 'ro', isa =&gt; 'Str');
	
	sub login {
		my $self = shift;
		print $self-&gt;name, " is logged in\n";
	}
}

{
	package Tracing;
	use MooseX::Aspect;
	
	my $aspect = __PACKAGE__;
	
	sub log_msg {
		my $class = shift;
		print STDERR "TRACE: $_\n" for @_;
	}
	
	apply_to 'User' =&gt; role {
		before login =&gt; sub {
			my $self = shift;
			$aspect-&gt;log_msg($self-&gt;name . " attempted to log in");
		};
	};
}

# try setting tracing to 0 :-)
my $config = { tracing =&gt; 1 };

if ($config-&gt;{tracing}) {
	Tracing-&gt;setup;
}

my $bob = User-&gt;new( name =&gt; 'Bob' );
$bob-&gt;login;
&lt;/code&gt;

&lt;p&gt;Packages of interest: [mod://Moose], [mod://MooseX::Aspect], [mod://Sub::Talisman] and [mod://MooseX::ModifyTaggedMethods].&lt;/p&gt;

&lt;p&gt;MooseX::Aspect is still at a very early stage of development, and bugs abound. But I think it shows quite a bit of promise for doing this sort of thing.&lt;/p&gt;

&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-757127"&gt;
&lt;small&gt;&lt;small&gt;
&lt;tt&gt;perl -E'sub Monkey::do{say$_,for@_,do{($monkey=&amp;#x5B;caller(0)]-&gt;&amp;#x5B;3])=~s{::}{ }and$monkey}}"Monkey say"-&gt;Monkey::do'
&lt;/tt&gt;&lt;/small&gt;&lt;/small&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1000101</field>
<field name="parent_node">
1000101</field>
</data>
</node>
