Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Comment on

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

Hook::LexWrap can do some deeply funky things. It has been particularly helpful to me when I've needed to add instrumentation to code during debugging and testing.

Consider the following class:

package Foo; sub new { bless {todo => 0, done => 0}, shift }; sub add { shift->{todo}++ }; sub remove { my $self = shift; $self->{todo}--; $self->{done}++; }; sub todo { shift->{todo} }; sub done { shift->{done} };

Nice and simple. Unless of course some bad classes break encapsulation:

package Bar; use base qw(Foo); # oops. $self->{done} not updated sub naughty_remove { shift->{todo}-- }; ... package Ni; use base qw(Foo); sub naughty_transfer { my ($to, $from) = @_; while ($from->todo) { $to->{todo}++; # oops. $from->{done} not updated $from->{todo}--; }; };

This kind of bug can be hard to locate in large classes. Wouldn't it be nice if we could ask perl to keep a lookout for where Foo objects are used incorrectly by a subroutine? Maybe something like this:

# Monitor the arguments of subroutines ... my $monitor = monitor_arg # ... in the following packages ... in_package => [ 'Foo', 'Bar', 'Ni' ], # ... where the argument isa Foo object matching => sub { ref($_[0]) && UNIVERSAL::isa($_[0], 'Foo') }, # ... and check that we didn't mess with the internals ... with => sub { my ($original, $current, $subroutine) = @_; my $removed = $original->todo - $current->todo; my $done = $current->done - $original->done; warn "removed item not added to done list in $subroutine\n" if $removed > 0 && $removed != $done; };

With the wonder of Hook::LexWrap we can :-)

use Carp; use Storable qw(dclone); use Hook::LexWrap; use Devel::Symdump; our $Monitoring; sub monitor_arg { my %param = @_; my ($matching, $in_package, $with) = map {$param{$_} or croak "need $_"} qw(matching in_package with); croak "called in void context" unless defined wantarray; my @wrappers; foreach my $subroutine (Devel::Symdump->functions(@$in_package)) { my ($original, @current); push @wrappers, wrap $subroutine, pre => sub { return if $Monitoring; local $Monitoring = 1; @current = grep { $matching->($_) } @_; $original = dclone(\@current); }, post => sub { return if $Monitoring; local $Monitoring = 1; foreach my $current (@current) { my $original = shift @$original; $with->($original, $current, $subroutine); } }; }; return \@wrappers; };

So running:

my $o = Bar->new; $o->add; $o->add; $o->naughty_remove; my $from = Foo->new(); $from->add; my $to = Ni->new; $to->naughty_transfer($from);

with the previously shown call to monitor_arg will give us

removed item not added to done list in Bar::naughty_remove removed item not added to done list in Ni::naughty_transfer

I'm sure you get the idea. What interesting things have you done with Hook::LexWrap?


In reply to Fun with Hook::LexWrap and code instrumentation by adrianh

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (11)
    As of 2014-08-20 22:59 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (125 votes), past polls