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

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??

This looks pretty slick.

I'd probably put some more abstraction into the interface of the loader function. Also, you shouldn't need two steps to create a backup and install a new sub.

#!/usr/bin/perl package test_package; use strict; use warnings; sub new { return bless {}; } sub test_function { print "Original test_function! $_[1]\n"; } sub loader { my $self = shift; my $override = shift; # Name of function to override. my $new_function = shift; # code ref to install my $backup; # keep a code ref of the original here. local @_; eval { no strict 'refs'; no warnings 'redefine'; die "Can't override non-existant function $override\n" unless exists &{$override}; $backup = \&{$override}; *{$override} = $new_function; }; if ( $@ ) { warn "ERROR: $@\n" if ( $@ ); $backup = undef; } return $backup; } package main; use strict; use warnings; my $counter = 0; my $f = new test_package(); $f->test_function($counter++); # 1 my $newsub = sub { print "New output! $_[1]+\n"; 1 }; # Replace function, get backup in return. my $backup = $f->loader( 'test_package::test_function', $newsub ); $f->test_function($counter++); # Restore function. $f->loader( "test_package::test_function", $backup); $f->test_function($counter++); my $faked =$f->loader( "test_package::fake_function", $newsub ); warn "Unable to override function" unless $faked; #$f->fake_function( 'Fake'); # program will die

If you specifically want to focus on overriding and restoring functions, you could actually use your object to store the names of overridden functions and their original code refs. Then your code could look like this:

$f->override( 'test::function', sub { print 'foo' } ); # Do your testing; $f->restore( 'test::function' );

Update: You might also want to look at Sub::Installer.

TGI says moo

In reply to Re: "Backing up" a subroutine during runtime. by TGI
in thread "Backing up" a subroutine during runtime. by dextius

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.