Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Rewriting sub code on import to add logging

by snoopy (Deacon)
on May 14, 2008 at 01:12 UTC ( #686422=note: print w/ replies, xml ) Need Help??


in reply to Rewriting sub code on import to add logging

You might be interested in Attribute::Handlers as a way of installing handlers base on subroutine attributes:

#!/usr/bin/perl use warnings; use strict; use Attribute::Handlers; sub log : ATTR(CODE) { my ($pkg, $sym, $code) = @_; no strict 'refs'; no warnings 'redefine'; # # Install log handler # my $name = *{ $sym }{NAME}; warn "installing log handler for $pkg\:\:$name"; *{$pkg . '::' . $name} = sub { warn "BEGIN sub block...$pkg\:\:$name\n"; my @rv = $code->(@_); warn "END sub block...$pkg\:\:$name\n\n"; return @rv; } } sub something : log { print "I'm doing something with @_.\n"; return 42; } my ($result) = something(qw/foo bar/); print "result: $result\n";
This produces:
installing log handler for main::something at log.pl line 13. BEGIN sub block...main::something I'm doing something with foo bar. END sub block...main::something result: 42
Update: Added no warnings 'redefine';


Comment on Re: Rewriting sub code on import to add logging
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://686422]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (14)
As of 2015-07-30 14:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls