Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Greetings all,

I've got an application with a handful of modules in an inheritance tree, with a single super-class above it all from which the other modules eventually inherit, directly or indirectly. What I'd like to do is add in every sub a call to a logging method without having lots of duplicated code. So I thought what might work is to use the super-class's import() to find all the subroutines within the package and rewrite them, adding the call. I've failed miserably.

Ultimately, here's what I'd like to have happen:

sub something { my ( $self, $param ) = @_; return _stuff($param); }

...gets rewritten to...

sub something { log_msg('starting sub'); my ( $self, $param ) = @_; return _stuff($param); }

So far, this is what I've come up with (but I can't get it to work). This is import() from the super-class:

sub import { my $package_name = shift; if ( $package_name !~ /^Application::Stuff_I_Dont_Want_To_Alter::/ ) { no strict 'refs'; no warnings 'redefine'; foreach ( keys %{ *{ $package_name . '::' } } ) { next if ( $_ !~ /[a-z]/ or $_ =~ /::$/ or $_ eq 'isa' or $_ eq 'import' ); if ( *{ $package_name, '::', $_ }{'CODE'} ) { my $code = do{ *{ $package_name, '::', $_ }{'CODE'}; }; *{ $package_name, '::', $_ } = sub { warn "BEGIN sub block...\n"; my @rv = $code->(@_); warn "END sub block...\n"; return @rv; }; } } } }

So part of the problem (apart from my inability to get this to work at all) is that the warn statements are outside the $code->() call. Is there a way I can append them inside the subroutine itself?

code('Perl') || die;

In reply to Rewriting sub code on import to add logging by gryphon

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (3)
    As of 2020-11-28 05:08 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found