Matt S Trout > Class-MethodFilter-0.02 > Class::MethodFilter



Annotate this POD

View/Report Bugs
Module Version: 0.02   Source  


Class::MethodFilter - add filters to class methods


  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!"


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.


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');

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


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.


Matt S Trout <>


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

syntax highlighting: