The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Sub::Mage - Override, Restore and manipulate subroutines 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 minimal 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. Sub::Mage now has the ability to manipulate subroutines by creating after and before modifier hooks, or create new subroutines on the go with conjur. New debugging functionality has been added also. With sub_alert you can see when any subroutines (not Sub::Mage imported ones) are being called.

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!

INCANTATIONS

When you use Sub::Mage there are currently a couple of options you can pass to it. One is :5.010. This will import the 5.010 feature.. this has nothing to do with subs, but I like this module, so it's there. The other is :Debug. If for some reason you want some kind of debugging going on when you override, restore, conjur or create hook modifiers then this will enable it for you. It can get verbose, so use it only when you need to.

use Sub::Mage ':5.010';

say "It works!";

#--

use Sub::Mage qw/:5.010 :Debug/;

conjur 'asub' => sub { }; # notifies you with [debug] that a subroutine was conjured

Now with importing we can turn a perfectly normal package into a class, sort of. It saves you from creating sub new { ... }

# MyApp.pm
package MyApp;

use Sub::Mage qw/:5.010 :Class/;

1;

# test.pl
my $foo = MyApp->new;

MyApp->conjur( name => sub {
    my ($self, $name) = @_;

    $self->{name} = $name;
    say "Set name to $name";
});

MyApp->conjur( getName => sub { return shift->{name}; });

$foo->name('World');

say $foo->getName;

Above we created a basically blank package, passed :Class to the Sub::Mage import method, then controlled the entire class from test.pl. As of 0.007, :Class now offers augmentation using augment which inherits a specified class.

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!

conjur

"Conjurs" a subroutine into the current script or a class. By conjur I just mean create. It will not allow you to override a subroutine using conjur.

conjur 'test' => sub { print "In test\n"; }
test;

Foo->conjur( hello => sub {
    my ($self, $name) = @_;

    print "Hello, $name!\n";
});

sub_alert

Very verbose: Adds a before hook modifier to every subroutine in the package to let you know when a sub is being called. Great for debugging if you're not sure a method is being ran.

__PACKAGE__->sub_alert;

# define a normal sub
sub test { return "World"; }

say "Hello, " . test(); # prints Hello, World but also lets you know 'test' in 'package' was called.

duplicate

Duplicates a subroutine from one class to another. Probably rarely used, but the feature is there if you need it.

use ThisPackage;
use ThatPackage;

duplicate 'subname' => ( from => 'ThisPackage', to => 'ThatPackage' );

ThatPackage->subname; # duplicate of ThisPackage->subname

augment

To use augment you need to have :Class imported. Augment will extend the given class thereby inheriting it into the current class.

package Spell;

sub lightning { }

1;

package Magic;

use Sub::Mage qw/:Class/;
augment 'Spell';

override 'lightning' => sub { say "Zappo!" };
Magic->lightning;

1;

The above would not have worked if we had not have augmented 'Spell'. This is because when we inheritted it, we also got access to its lightning method.

exports

Exporting subroutines is not generally needed or a good idea, so Sub::Mage will only allow you to export one subroutine at a time. Once you export the subroutine you can call it into the given package without referencing the class of the subroutines package.

package Foo;

use Sub::Mage;

exports 'boo' => ( into => [qw/ThisClass ThatClass/] );

sub boo { print "boo!!!\n"; }
sub test { print "A test\n"; }

package ThisClass;

use Foo;

boo(); # instead of Foo->boo;
test(); # this will fail because it was not exported

have

A pretty useless function, but it may be used to silently error, or create custom errors for failed subroutines. Similar to $class->can($method), but with some extra sugar.

package Foo;

use Sub::Mage;

sub test { }

package MyApp;

use Sub::Mage qw/:5.010/;

use Foo;

my $success = sub {
    my ($class, $name) = @_;
  
    say "$class\::$name checked out OK";  
    after $class => sub {
        say "Successfully ran $name in $class";
    };
};

Foo->have( 'test' => ( then => $success ) );

On success the above will run whatever is in then. But what about errors? If this fails it will not do anything - sometimes you just want silent deaths, right? You can create custom error handlers by using or. This parameter may take a coderef or a string.

package Foo;

use Sub::Mage;

sub knife { }

package MyApp;

use Sub::Mage qw/:5.010/;

use Foo;

my $error = sub {
    my ($class, $name) = @_;

    say "Oh dear! $class failed because no method $name exists";
    # do some other funky stuff if you wish
};

Foo->have( 'spoon' => ( then => $success, or => $error ) );

Or you may wish for something really simply.

Foo->have( 'spoon' => ( then => $success, or => 'There is no spoon') );

This one will simply thrown a warning with warn so to still execute any following code you may have.

AUTHOR

Brad Haywood <brad@geeksware.net>

LICENSE

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