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

Class::Method::Modifiers - provides Moose-like method modifiers

VERSION

Version 0.05 released 17 Aug 07

SYNOPSIS

    package Child::Class;
    use parent 'Parent::Class';
    use Class::Method::Modifiers;

    sub new_method { }

    before 'old_method' => sub
    {
        carp "old_method is deprecated, use new_method";
    };

    around 'other_method' => sub
    {
        my $orig = shift;
        my ($self, @args) = @_;

        my $ret = $orig->($self, @args);
        return $ret =~ /\d/ ? $ret : lc $ret;
    };

MODIFIERS

All three modifiers; before, after, and around; are exported into your namespace by default. You may use Class::Method::Modifiers () to avoid thrashing your namespace. I may steal more features from Moose, namely super, override, inner, augment, and whatever the Moose folks come up with next.

Note that the syntax and semantics for these modifiers is directly borrowed from Moose (the implementations, however, are not).

Parent classes need not know about Class::Method::Modifiers. This means you should be able to modify methods in any subclass.

before method(s) => sub { ... }

before is called before the method it is modifying. Its return value is totally ignored. It receives the same @_ as the the method it is modifying would have received. You can modify the @_ the original method will receive by changing $_[0] and friends (or by changing anything inside a reference). This is a feature!

after method(s) => sub { ... }

after is called after the method it is modifying. Its return value is totally ignored. It receives the same @_ as the the method it is modifying received, mostly. The original method can modify @_ (such as by changing $_[0] or references) and after will see the modified version. If you don't like this behavior, specify both a before and after, and copy the @_ during before for after to use.

around method(s) => sub { ... }

around is called instead of the method it is modifying. The method you're overriding is passed in as the first argument (called $orig by convention). Watch out for contextual return values of $orig.

You can use around to:

Pass $orig a different @_
    around 'method' => sub
    {
        my $orig = shift; my $self = shift;
        $orig->($self, reverse @_);
    };
Munge the return value of $orig
    around 'method' => sub
    {
        my $orig = shift;
        ucfirst $orig->(@_);
    };
Avoid calling $orig -- conditionally
    around 'method' => sub
    {
        my $orig = shift;
        return $orig->(@_) if time() % 2;
        return "no dice, captain";
    };

CAVEATS

It is erroneous to modify a method that doesn't exist in your class's inheritance hierarchy. If this occurs, an exception will be thrown when the method is invoked.

It doesn't yet play well with caller. There are some todo tests for this.

SEE ALSO

Moose, Class::MOP::Method::Wrapped, MRO::Compat, CLOS

AUTHOR

Shawn M Moore, <sartak at gmail.com>

BUGS

Calling $orig twice in an around modifier is prone to breakage. Moose supports this, I currently don't.

Please report any bugs through RT: email bug-class-method-modifiers at rt.cpan.org, or browse to http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-Method-Modifiers.

SUPPORT

You can find this documentation for this module with the perldoc command.

    perldoc Class::Method::Modifiers

You can also look for information at:

ACKNOWLEDGEMENTS

Thanks to Stevan Little for Moose, I would never have known about method modifiers otherwise.

Thanks to Matt Trout and Stevan Little for their advice.

COPYRIGHT & LICENSE

Copyright 2007 Shawn M Moore.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.