The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Class::MethodFilter - add filters to class methods

SYNOPSIS

  package SomeClass;

  sub foo {
    return "foo!\n";
  }

  __PACKAGE__->add_method_filter('foo', sub { $_[1] =~ tr/a-z/A-Z/; $_[1]; });

  # Meanwhile, in another piece of code ...

  my $obj = new SomeClass;
  print $obj->foo();             # Prints "foo!"
  print $obj->foo_filtered();    # Prints "FOO!"

DESCRIPTION

Class::MethodFilter is designed for situations where you want a filtered version of the return values of a method that's separate from the method itself; it provides a convenient API for installing filters written different ways while leaving the eventual interface consistent.

DETAILS

A single additional class method is added to your package, add_method_filter. It can be called with (so far) three different signatures -

  __PACKAGE__->add_method_filter('method', sub { ... } );
  __PACKAGE__->add_method_filter('method', 'filter_method');
  __PACKAGE__->add_method_filter('method');

The first form installs the supplied sub as __PACKAGE__::method_filter, the second creates an __PACKAGE__::method_filter that proxies the call to the named method, and the third assumes that __PACKAGE__::method_filter already exists.

If __PACKAGE__::method_filtered does not exist, one is created that calls

  $_[0]->method_filter($_[0]->method(@_));

and returns the result. If it *does* exist, it is assumed to have accessor-like behaviour (as from e.g. use of Class::Accessor) and Class::MethodFilter replaces __PACKAGE__::method with a sub that calls $_[0]->method_filtered(@_) and then returns the result of invoking the original method. This is designed to allow __PACKAGE__::method_filtered to act as a cache for the filtered result which is automatically updated every time the method it's filtering is called.

AUTHOR

Matt S Trout <mstrout@cpan.org>

LICENSE

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