Sub::Methodical - call methods as functions
Version 0.002
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); }
Don't you get tired of typing $self-> all the time when you're calling your methods?
$self->
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.
:Methodical
-auto
$self
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.
isa
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.
bar
foo
foo()
$self->foo
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.
AUTOLOAD
In other words, if you want to write a subclass that continues to call (inherited) methods as functions, you need to use this.
This is exported to grab the :Methodical attribute.
This is exported by the -inherit import argument.
Hans Dieter Pearcey, <hdp at cpan.org>
<hdp at cpan.org>
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.
bug-sub-methodical at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Sub::Methodical
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Sub-Methodical
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Sub-Methodical
CPAN Ratings
http://cpanratings.perl.org/d/Sub-Methodical
Search CPAN
http://search.cpan.org/dist/Sub-Methodical
Thanks to Ricardo SIGNES for having this idea.
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.
To install Sub::Methodical, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Sub::Methodical
CPAN shell
perl -MCPAN -e shell install Sub::Methodical
For more information on module installation, please visit the detailed CPAN module installation guide.