NAME

Sub::Methodical - call methods as functions

VERSION

Version 0.002

SYNOPSIS

package My::Module;
use Sub::Methodical;

sub foo :Methodical { ... do stuff ... }

sub bar {
  my ($self, $arg) = @_;
  
  # this secretly grabs the current scope's $self
  foo($arg, @more_args);

  # ... this is identical
  $self->foo($arg, @more_args);
}

DESCRIPTION

Don't you get tired of typing $self-> all the time when you're calling your methods?

Now you don't have to anymore. Any function you give the :Methodical attribute (or, with the -auto import argument, any function that doesn't start with '_') is automatically called as a method whenever you call it as a function, taking its invocant ($self) from the calling scope.

USE

The :Methodical Attribute

This attribute marks a single function as a Methodical method. Once marked, these two invocations are identical:

sub foo :Methodical { ... }

sub bar {
  my ($self) = @_;
  foo();
  $self->foo;
}

There must be a lexical variable named $self in the function that calls a Methodical method, and it must be blessed into a class that isa the package that the method was originally defined in.

Methods called as functions still behave like normal methods as far as subclassing and overriding goes. That is, given the example above, if a subclass contained this code,

sub foo { ... something else ... }

that subclass' bar method would continue to work, and would call the correct (subclass) foo method, even when it was called as foo() instead of $self->foo.

-auto

use Sub::Methodical -auto;

This argument tells Sub::Methodical to look for all functions defined in the current package (whose names do not begin with '_') and treat them as though they had the :Methodical attribute.

-inherit

use Sub::Methodical -inherit;

This argument installs an AUTOLOAD function that will perform redispatch to inherited :Methodical methods.

In other words, if you want to write a subclass that continues to call (inherited) methods as functions, you need to use this.

EXPORTS

MODIFY_CODE_ATTRIBUTES

This is exported to grab the :Methodical attribute.

AUTOLOAD

This is exported by the -inherit import argument.

AUTHOR

Hans Dieter Pearcey, <hdp at cpan.org>

BUGS

Please report any bugs or feature requests to bug-sub-methodical at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Sub-Methodical. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

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

perldoc Sub::Methodical

You can also look for information at:

ACKNOWLEDGEMENTS

Thanks to Ricardo SIGNES for having this idea.

COPYRIGHT & LICENSE

Copyright 2007 Hans Dieter Pearcey, all rights reserved.

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