Sub::Installer - A cleaner way to install (or reinstall) package subroutines
This document describes Sub::Installer version 0.0.3
use Sub::Installer; $installed_ref = PackageName->install_sub({ subname => $sub_ref }); $installed_ref = PackageName->install_sub({ subname => $sub_code_str }); $installed_ref = PackageName->reinstall_sub({subname => $other_sub_ref });
This module provides two universal methods that any package/class can use to install subroutines in its own namespace.
PackageName->install_sub(\%sub_specs)
This method installs one or more subroutines in the package on which it's invoked. Each subroutine is specified as an entry in the hash whose reference is passed to the method. The key of each entry is the name of the subroutine, and the corresponding value is either a subroutine reference, or a character string containing the source code of the body of the subroutine.
For example:
use Sub::Installer; MyClass->install_sub({ new => sub { bless { @[1..$#_] }, $_[0] } }); # or, equivalently... MyClass->install_sub({ new => q{ bless { @[1..$#_] }, $_[0] } });
In either case, the method call returns a reference to the last subroutine that was installed (though this is generally only useful if you're installing a single subroutine).
In other words, calling:
$ref = $PackageName->install_sub({ $sub_name => $sub_ref });
is just a cleaner and more maintainable way of writing:
$ref = do { no strict 'refs'; *{$PackageName.'::'.$subname} = $sub_ref; }
PackageName->reinstall_sub(\%sub_specs)
This method acts exactly like install_sub() except that it suppresses any Subroutine subname redefined warnings.
install_sub()
Subroutine subname redefined
That is, calling:
$PackageName->reinstall_sub({ $sub_name => $sub_ref });
do { no warnings 'redefine'; no strict 'refs'; *{$PackageName.'::'.$subname} = $sub_ref; }
You tried to call install_sub() or reinstall_sub() with the wrong number of arguments, or without passing it a hash reference specifying the subroutines to be installed. A common mistake is to pass raw key/value pairs, without putting them in a hash first.
reinstall_sub()
You passed a string instead of a subroutine reference, but that string wasn't a valid subroutine body. The compiler's actual problem with the code appears after the colon.
Sub::Installer requires no configuration files or environment variables.
None.
None reported.
No bugs have been reported.
Please report any bugs or feature requests to bug-sub-installer@rt.cpan.org, or through the web interface at http://rt.cpan.org.
bug-sub-installer@rt.cpan.org
Damian Conway <DCONWAY@cpan.org>
<DCONWAY@cpan.org>
Copyright (c) 2005, Damian Conway <DCONWAY@cpan.org>. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
To install Sub::Installer, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Sub::Installer
CPAN shell
perl -MCPAN -e shell install Sub::Installer
For more information on module installation, please visit the detailed CPAN module installation guide.