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

NAME

Sub::Installer - A cleaner way to install (or reinstall) package subroutines

VERSION

This document describes Sub::Installer version 0.0.3

SYNOPSIS

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

  

DESCRIPTION

This module provides two universal methods that any package/class can use to install subroutines in its own namespace.

INTERFACE

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.

That is, calling:

    $PackageName->reinstall_sub({ $sub_name => $sub_ref });

is just a cleaner and more maintainable way of writing:

    do {
        no warnings 'redefine';
        no strict 'refs';
        *{$PackageName.'::'.$subname} = $sub_ref;
    }

DIAGNOSTICS

Usage: PackageName->install_sub( \%sub_def )"
Usage: PackageName->reinstall_sub( \%sub_def )

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.

Can't install invalid code: %s

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.

CONFIGURATION AND ENVIRONMENT

Sub::Installer requires no configuration files or environment variables.

DEPENDENCIES

None.

INCOMPATIBILITIES

None reported.

BUGS AND LIMITATIONS

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.

AUTHOR

Damian Conway <DCONWAY@cpan.org>

LICENCE AND COPYRIGHT

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.

DISCLAIMER OF WARRANTY

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.