# man > Class::Method::Modifiers(3pm)

---
type: CommandReference
command: Class::Method::Modifiers
mode: perldoc
section: 3pm
source: perldoc
---

## Quick Reference

- `before 'method' => sub { ... }` — run code before method, receives same `@_`
- `after 'method' => sub { ... }` — run code after method, sees modified `@_`
- `around 'method' => sub { my $orig = shift; ... }` — replace method, call original via `$orig`
- `fresh 'method' => sub { ... }` — install method only if it doesn't exist in hierarchy
- `install_modifier($package, $type, @names, sub { ... })` — install modifier dynamically

## Name

Provides Moose-like method modifiers.

## Synopsis

perl
package Child;
use parent 'MyParent';
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 $ret = $orig->(@_);
    return $ret =~ /\d/ ? $ret : lc $ret;
};

after 'private', 'protected' => sub {
    debug "finished calling a dangerous method";
};

use Class::Method::Modifiers qw(fresh);

fresh 'not_in_hierarchy' => sub {
    warn "freshly added method\n";
};
## Modifiers

All modifiers can modify one or multiple methods at once. Names can be a list or arrayref.

perl
before 'method' => sub { ... };
before 'method1', 'method2' => sub { ... };
before [ 'method1', 'method2' ] => sub { ... };
- `before method(s) => sub { ... }` — called before the original method. Return value ignored. Receives same `@_`; can modify `@_` for original method.
- `after method(s) => sub { ... }` — called after the original method. Return value ignored. Sees any modifications to `@_` made by the original method.
- `around method(s) => sub { ... }` — called instead of the original method. Original method passed as first argument (`$orig`). Use `$orig->(@_)` to invoke original. Can pass different `@_`, munge return value, or conditionally avoid calling `$orig`.
- `fresh method(s) => sub { ... }` — installs the coderef as a method only if no method of that name exists in the class hierarchy. Throws exception if conflict. Exported only on request or `:all`.
- `install_modifier($package, $type, @names, sub { ... })` — dynamically install modifier in specified package. Exported only on request or `:all`. `$type` is `'before'`, `'after'`, `'around'`, or `'fresh'`.

## Examples

**Using `around` to pass different arguments:**

perl
around 'method' => sub {
    my $orig = shift;
    my $self = shift;
    $orig->($self, reverse @_);
};
**Munging return value with `around`:**

perl
around 'method' => sub {
    my $orig = shift;
    ucfirst $orig->(@_);
};
**Conditionally avoiding original method:**

perl
around 'method' => sub {
    my $orig = shift;
    return $orig->(@_) if time() % 2;
    return "no dice, captain";
};
**Using `fresh` for safe subclassing:**

perl
package My::Subclass;
use base 'Some::Base';
use Class::Method::Modifiers 'fresh';

fresh 'foo' => sub { ... };
## See Also

- [Class::Method::Modifiers::Fast](http://localhost/phpMan.php/perldoc/Class%3A%3AMethod%3A%3AModifiers%3A%3AFast/markdown)
- Moose
- [Class::Trigger](http://localhost/phpMan.php/perldoc/Class%3A%3ATrigger/markdown)
- [Class::MOP::Method::Wrapped](http://localhost/phpMan.php/perldoc/Class%3A%3AMOP%3A%3AMethod%3A%3AWrapped/markdown)
- [MRO::Compat](http://localhost/phpMan.php/perldoc/MRO%3A%3ACompat/markdown)
- CLOS (<https://en.wikipedia.org/wiki/Common_Lisp_Object_System>)