http://www.perlmonks.org?node_id=1000131


in reply to Core module: Attribute::Handlers behaviour

Perl attributes are typically handled at CHECK time - between compile time (BEGIN) and INIT time, and way before run time. Your eval happens at run time, so too late to effect attribute processing.

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.

* * *

As it happens I've been working on some aspect-oriented programming stuff for Moose that might be just what you need.

{ package User; use Moose; has name => (is => 'ro', isa => 'Str'); sub login { my $self = shift; print $self->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' => role { before login => sub { my $self = shift; $aspect->log_msg($self->name . " attempted to log in"); }; }; } # try setting tracing to 0 :-) my $config = { tracing => 1 }; if ($config->{tracing}) { Tracing->setup; } my $bob = User->new( name => 'Bob' ); $bob->login;

Packages of interest: Moose, MooseX::Aspect, Sub::Talisman and MooseX::ModifyTaggedMethods.

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.

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