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'

Replies are listed 'Best First'.
Re^2: Core module: Attribute::Handlers behaviour
by BrowserUk (Patriarch) on Oct 20, 2012 at 16:20 UTC
    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.

    Hm. That doesn't ring true.

    Just because it is the main scripts runtime, doesn't prevent the eval use'd script from having its own BEGIN & END times. After all, it still has to be compiled.

    Given junk.pm:

    package junk; sub BEGIN { printf "Hi from %s BEGIN\n", __PACKAGE__ } sub CHECK { printf "Hi from %s CHECK\n", __PACKAGE__ } sub INIT { printf "Hi from %s INIT\n", __PACKAGE__ } sub END { printf "Hi from %s END\n", __PACKAGE__ } 1;

    This script that eval's it into being:

    #! perl -slw use strict; print "Main runtime: About to eval use junk"; <STDIN>; eval "use junk"; print "Main runtime: Just eval'd use junk"; <STDIN>;

    I get:

    C:\test>junk Main runtime: About to eval use junk Hi from junk BEGIN Too late to run CHECK block at junk.pm line 4, <STDIN> line 1. Too late to run INIT block at junk.pm line 5, <STDIN> line 1. Main runtime: Just eval'd use junk Hi from junk END

    So, if the attribute handling can be done at BEGIN time, then it is quite simple to wrap the attributed function over in a wrapper that did the tracing,

    Tweak junk.pm above to be::

    package junk; use Attribute::Handlers; no warnings 'redefine'; sub TRACE : ATTR(CODE,BEGIN) { my ($pkg, $sym, $ref, $attr, $data, $phase, $file, $line) = @_; *{ $sym } = sub { warn "$file($line) called with [@_]\n"; my( @rc )= &$ref; warn "$file($line): returning [ @rc ]\n"; } } sub doStuff :TRACE { print "junk::doStuff says hi"; } 1;

    And main to be:

    #! perl -slw use strict; print "Main runtime: About to eval use junk"; <STDIN>; eval "use junk"; print "Main runtime: Just eval'd use junk; calling doStuff()"; <STDIN> +; junk::doStuff( 1..10 );

    And you get:

    C:\test>junk Main runtime: About to eval use junk Main runtime: Just eval'd use junk; calling doStuff() junk.pm(16) called with [1 2 3 4 5 6 7 8 9 10] junk::doStuff says hi junk.pm(16): returning [ 1 ]

    which is kind of cool. (Shame the function name isn't provided to the attribute handler.)

    It'd be better if installing the TRACE attr into UNIVERSAL would then apply the wrappers to runtime loaded modules; and better yet if you you could use a "trace handler" package once in main and have it apply itself everywhere.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

    @_

      "Just because it is the main scripts runtime, doesn't prevent the eval use'd script from having its own BEGIN & END times. After all, it still has to be compiled."

      Naturally. But it was my reading of the OP that he wanted to be able to apply the attributes defined in the eval-use'd script to subs defined in its caller, which would have already finished compiling. Of course, I may have misinterpreted.

      "Shame the function name isn't provided to the attribute handler."

      That's why god invented Sub::Identify ;-)

      1066 and all that