NAME

Sub::Mage - Override and Restore subs on-the-fly, without magic.

DESCRIPTION

On the very rare occasion you may need to override a subroutine for any particular reason. This module will help you do that with minimum fuss. Afterwards, when you're done, you can simply restore the subroutine back to its original state. Used on its own will override/restore a sub from the current script, but called from a class it will alter that classes subroutine. As long as the current package has access to it, it may be altered.

SYNOPSIS

# Single file

use Sub::Mage;

sub greet { print "Hello, World!"; }

greet; # prints Hello, World!

override 'greet' => sub {
    print "Goodbye, World!";
};

greet; # now prints Goodbye, World!

restore 'greet'; # restores it back to its original state

Changing a class method, by example

# Foo.pm

use Sub::Mage;

sub new { my $self = {}; return bless $self, __PACKAGE__; }

sub hello {
    my $self = shift;

    $self->{name} = "World";
}

# test.pl

use Foo;

my $foo = Foo->new;

Foo->override( 'hello' => sub {
    my $self = shift;

    $self->{name} = "Town";
});

print "Hello, " . $foo->hello . "!\n"; # prints Hello, Town!

Foo->restore('hello');

print "Hello, " . $foo->hello . "!\n"; # prints Hello, World!

Spells

override

Overrides a subroutine with the one specified. On its own will override the one in the current script, but if you call it from a class, and that class is visible, then it will alter the subroutine in that class instead. Overriding a subroutine inherits everything the old one had, including $self in class methods.

override 'subname' => sub {
    # do stuff here
};

# class method
FooClass->override( 'subname' => sub {
    my $self = shift;

    # do stuff
});

restore

Restores a subroutine to its original state.

override 'foo' => sub { };

restore 'foo'; # and we're back in the room

after

Adds an after hook modifier to the subroutine. Anything in the after subroutine is called directly after the original sub. Hook modifiers can also be restored.

sub greet { print "Hello, "; }

after 'greet' => sub { print "World!"; };

greet(); # prints Hello, World!

before

Very similar to after, but calls the before subroutine, yes that's right, before the original one.

sub bye { print "Bye!"; }

before 'bye' => sub { print "Good "; };

bye(); # prints Good Bye!

AUTHOR

Brad Haywood <brad@geeksware.net>

LICENSE

You may distribute this code under the same terms as Perl itself.