Not the most versatile option, but thought I'd throw it out there. I wrote Wrap::Sub quite a while ago as a learning experience that can do this kind of thing. It works by running code before the actual sub is called where you can access the incoming parameters then do work before the actual sub is called, then runs another chunk of code after the real sub is called, where the return value(s) of the sub are received (which again, you can do more work on if desired).

You must list all of the subs you want to wrap (if subs in other packages are desired, specify the full package name of the sub (eg 'Foo::Bar::sub_name').

use warnings; use strict; use Data::Dumper; use Wrap::Sub; my $ws = Wrap::Sub->new( pre => sub { print "\n$Wrap::Sub::name\n"; print "args:\n"; print Dumper \@_; }, post => sub { print "return:\n"; print Dumper $_[1][0]; } ); my @wrapped; for (qw(foo bar)){ push @wrapped, $ws->wrap($_); } foo(a => 1, b => 2); bar(10); sub foo { my %hash = @_; return [1, 2, 3]; } sub bar { my $x = shift; return $x ** 2; }

Output:

main::foo args: $VAR1 = [ 'a', 1, 'b', 2 ]; return: $VAR1 = [ 1, 2, 3 ]; main::bar args: $VAR1 = [ 10 ]; return: $VAR1 = 100;

Of course, the formatting of the output isn't very creative, but that's simple to fix to your liking within the pre and post subs (which are simple code references, so you can define them anywhere, not necessarily within the init call). Note that you can also wrap all subs within a module in one pass as well: my $module_subs = $ws->wrap('My::Module');, so you can selectively wrap entire modules in a loop as opposed to individual subs like I did in the above example.


In reply to Re: Tool to record subroutine calls & return values by stevieb
in thread Tool to record subroutine calls & return values by Yary

Title:
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, details, 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, summary, 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.