Coder Social home page Coder Social logo

sub-monkey's Introduction

NAME
    Sub::Monkey - Dynamically and neatly monkey patch a module

DESCRIPTION
    In some cases, rare cases, you may need to temporarily patch a module
    on-the-go. Sub::Monkey can help you achieve this by providing a set of
    methods to create, override and add hook modifiers, similar to Moose,
    but can apply them to remote modules (Not the current one). This type of
    monkey patching is reasonably safe because you can plainly see what
    changes are being made to what modules. Obviously monkey patching isn't
    always the best alternative, but sometimes you may have no other choice.
    Sub::Monkey also provides the ability to undo any patching you made with
    "unpatch".

SYNOPSIS
        use Sub::Monkey qw<Some::Package>;

        method 'needThisMethod' => sub {
            ...
        },
        qw<Some::Package>;

    We just created a brand new method in the Some::Package class. If you
    attempt to override an existing method using "method", then Sub::Monkey
    will raise an error, because really you should be using "override"
    instead. Remember, to patch a module with Sub::Monkey, you need to
    explicitly tell it you want to modify a class by importing it when you
    "use Sub::Monkey". To do this for multiple modules just add them all
    into an array.

        use Sub::Monkey qw<Some::Package Foo::Bar Another::One>;

METHODS
  instance
    Patch an instance method instead of an entire class

        # Pig.pm
        package Pig;
        sub new { return bless {}, shift; }
        sub says { print "Oink!\n"; }

        # test.pl
        package main;
        use Sub::Monkey qw<Pig>;

        my $pig  = Pig->new;
        my $pig2 = Pig->new;
        instance 'says' => sub {
            print "Meow\n";
        },
        $pig2;

        # only $pig2 will have its says method overridden

  original
    If you want to run the original version of a patched method, but not
    unpatch it right away you can use "original" to do so. It will run the
    old method before it was patched with any arguments you specify, but the
    actual method will still remain patched.

        after 'someMethod' => sub {
            print "Blah\n"
        },
        qw<Foo>;

        original('Foo', 'someMethod', qw<these are my args>);

    OR if you prefer, you can just call
    Sub::Monkey::PatchedClassName::method->(@args)

        Sub::Monkey::Foo->someMethod('these', 'are', 'my', 'args);

  override
    Overrides an already existing method. If the target method doesn't exist
    then Sub::Monkey will throw an error.

        override 'foo' => sub {
            return "foo bar";
        },
        qw<Some::Module>;

  method
    Creates a brand new method in the target module. It will NOT allow you
    to override an existing one using this, and will throw an error.

        method 'active_customers' => sub {
            my $self = shift;
            return $self->search({ status => 'active' });
        },
        qw<Schema::ResultSet::Customer>;

  before
    Simply adds code to the target method before the original code is ran

        # Foo.pm
        package Foo;
    
        sub new { return bless {}, __PACKAGE__; }
        sub hello { print "Hello, $self->{name}; }
        1;

        # test.pl
        use Sub::Monkey qw<Foo>;
   
        my $foo = Foo->new; 
        before 'hello' => {
            my $self = shift;
            $self->{name} = 'World';
        },
        qw<Foo>;

        print $foo->hello . "\n";

  after
    Basically the same as "before", but appends the code specified to the
    END of the original

  around
    Around gives the user a bit more control over the subroutine. When you
    create an around method the first argument will be the original method,
    the second is $self and the third is any arguments passed to the
    original subroutine. In a away this allows you to control the flow of
    the entire subroutine.

        package MyFoo;

        sub greet {
            my ($self, $name) = @_;

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

        1;

        # test.pl

        use Sub::Monkey qw<MyFoo>;

        # only call greet if any arguments were passed to MyFoo->greet()
        around 'greet' => sub {
            my $method = shift;
            my $self = shift;

            $self->$method(@_)
                if @_;
        },
        qw<MyFoo>;

  unpatch
    Undoes any modifications made to patched methods, restoring it to its
    original state.

        override 'this' => sub { print "Blah\n"; }, qw<FooClass>;
  
        unpatch 'this' => 'FooClass';

AUTHOR
    Brad Haywood <[email protected]>

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

POD ERRORS
    Hey! The above document had some coding errors, which are explained
    below:

    Around line 15:
        Deleting unknown formatting code M<>

sub-monkey's People

Contributors

bradhaywood avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.