Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Core module: Attribute::Handlers behaviour

by enigma (Novice)
on Oct 21, 2012 at 10:11 UTC ( #1000192=note: print w/replies, xml ) Need Help??


in reply to Core module: Attribute::Handlers behaviour

Hello PerlMonks,

I understand the trick "BrowserUk" has suggested. I tried to implement the same as below but it is always giving me error as

Can't locate object method "new" via package "Module"

Module.pm

package Module; use Attribute::Handlers; 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 new :TRACE{ my $instance = shift; my $class = ref($instance) || $instance; my $self = {}; return bless($self, $class); } 1;

Test.pl

#!/usr/local/bin/perl my $module_to_load = 'Module'; eval "use $module_to_load;"; # Create object my $obj = Module->new(); print "CLASS: " . ref($obj) ."\n";

When i'm changing the execution phase in ATTR to INIT,CHECK - module gets compiled successfully but ATTR TRACE is not called. And when i change the phase to BEGIN, I'm getting the above error message.

I need your help to understand why my Test.pl is not able to find "new()" method in Module.pm when Attribute::Handers are called at BEGIN phase.

Thanks

Replies are listed 'Best First'.
Re^2: Core module: Attribute::Handlers behaviour
by Anonymous Monk on Oct 21, 2012 at 10:46 UTC

    I don't get that, even though your module doesn't return an object ( compared to Attribute::Constructor ), trace gets called if the phase is BEGIN

    $ perl -le " eval qq/use Junk;/; print Junk->new " Junk.pm(22) called with [Junk] Junk.pm(22): returning [ Junk=HASH(0x3f8d1c) ] 1 $ cat Junk.pm package Junk; use Attribute::Handlers; #~ sub TRACE :ATTR(CODE,CHECK) { #~ sub TRACE :ATTR(CODE,INIT,CHECK) { #~ sub TRACE :ATTR(CODE,INIT) { sub TRACE :ATTR(CODE,BEGIN) { #~ sub TRACE :ATTR(CODE) { 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 new :TRACE { my $instance = shift; my $class = ref($instance) || $instance; my $self = {}; return bless($self, $class); } 1;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000192]
help
Chatterbox?
[Discipulus]: why github use 'pull request' instead of 'push request'? where is the subject?
[Discipulus]: I'm the subject or is the program? i think i always am the subject
[erix]: it an invite to pull, as I understand it
[Discipulus]: Concave or convex?
[Discipulus]: who make the invitation? the program?!?
[choroba]: it's historical
[erix]: pull requests
[choroba]: originally, you sent a pull request to someone by email, i.e. you asked them to pull from your repo
[Discipulus]: if is the author of a patch, is more like a 'request to push' into rather than a pull request..
[choroba]: as it's considered unpolite to push to someone else's repo

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2017-05-27 20:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?