1 # Original package Sub::Install downloaded from CPAN on 6/15/2012. This module
2 # has been modified by Andreas Schuh on 6/15/2012 to make it part of BASIS.
4 package BASIS::Sub::Install;
14 BASIS::Sub::Install - install subroutines into packages easily
22 our $VERSION = '0.926';
26 use BASIS::Sub::Install;
28 BASIS::Sub::Install::install_sub({
36 This module makes it easy to install subroutines into packages without the
37 unslightly mess of C<no strict> or typeglobs lying about where just anyone can
44 BASIS::Sub::Install::install_sub({
46 into => "Finance::Shady",
50 This routine installs a given code reference into a package as a normal
51 subroutine. The above is equivalent to:
54 *{"Finance::Shady" . '::' . "launder"} = \&subroutine;
56 If C<into> is not given, the sub is installed into the calling package.
58 If C<code> is not a code reference, it is looked for as an existing sub in the
59 package named in the C<from> parameter. If C<from> is not given, it will look
60 in the calling package.
62 If C<as> is not given, and if C<code> is a name, C<as> will default to C<code>.
63 If C<as> is not given, but if C<code> is a code ref, BASIS::Sub::Install will try to
64 find the name of the given code ref and use that as C<as>.
66 That means that this code:
68 BASIS::Sub::Install::install_sub({
70 from => 'Person::InPain',
71 into => 'Person::Teenager',
77 package Person::Teenager;
79 BASIS::Sub::Install::install_sub({
80 code => Person::InPain->can('twitch'),
86 This routine behaves exactly like C<L</install_sub>>, but does not emit a
87 warning if warnings are on and the destination is already defined.
94 my $name = B::svref_2object($code)->GV->NAME;
95 return $name unless $name =~ /\A__ANON__/;
99 # See also Params::Util, to which this code was donated.
101 (Scalar::Util::reftype($_[0])||'') eq 'CODE'
102 || Scalar::Util::blessed($_[0])
103 && (overload::Method($_[0],'&{}') ? $_[0] : undef);
106 # do the heavy lifting
107 sub _build_public_installer {
108 my ($installer) = @_;
112 my ($calling_pkg) = caller(0);
114 # I'd rather use ||= but I'm whoring for Devel::Cover.
115 for (qw(into from)) { $arg->{$_} = $calling_pkg unless $arg->{$_} }
117 # This is the only absolutely required argument, in many cases.
118 Carp::croak "named argument 'code' is not optional" unless $arg->{code};
120 if (_CODELIKE($arg->{code})) {
121 $arg->{as} ||= _name_of_code($arg->{code});
124 "couldn't find subroutine named $arg->{code} in package $arg->{from}"
125 unless my $code = $arg->{from}->can($arg->{code});
127 $arg->{as} = $arg->{code} unless $arg->{as};
128 $arg->{code} = $code;
131 Carp::croak "couldn't determine name under which to install subroutine"
134 $installer->(@$arg{qw(into as code) });
144 Prototype\ mismatch:\ sub\ .+? |
145 Constant subroutine \S+ redefined
147 $_redef_warn_re = qr/Subroutine\ \S+\ redefined/x;
151 BEGIN { $eow_re = qr/ at .+? line \d+\.\Z/ };
155 my $code = delete $arg->{code};
156 my $wants_code = sub {
159 my $warn = $SIG{__WARN__} ? $SIG{__WARN__} : sub { warn @_ }; ## no critic
160 local $SIG{__WARN__} = sub {
162 for (@{ $arg->{suppress} }) {
163 return if $error =~ $_;
165 for (@{ $arg->{croak} }) {
166 if (my ($base_error) = $error =~ /\A($_) $eow_re/x) {
167 Carp::croak $base_error;
170 for (@{ $arg->{carp} }) {
171 if (my ($base_error) = $error =~ /\A($_) $eow_re/x) {
172 return $warn->(Carp::shortmess $base_error);
175 ($arg->{default} || $warn)->($error);
180 return $wants_code->($code) if $code;
186 my ($pkg, $name, $code) = @_;
187 no strict 'refs'; ## no critic ProhibitNoStrict
188 *{"$pkg\::$name"} = $code;
194 *_ignore_warnings = _do_with_warn({
195 carp => [ $_misc_warn_re, $_redef_warn_re ]
198 *install_sub = _build_public_installer(_ignore_warnings(_installer));
200 *_carp_warnings = _do_with_warn({
201 carp => [ $_misc_warn_re ],
202 suppress => [ $_redef_warn_re ],
205 *reinstall_sub = _build_public_installer(_carp_warnings(_installer));
207 *_install_fatal = _do_with_warn({
209 croak => [ $_redef_warn_re ],
213 =head2 install_installers
215 This routine is provided to allow BASIS::Sub::Install compatibility with
216 BASIS::Sub::Installer. It installs C<install_sub> and C<reinstall_sub> methods into
217 the package named by its argument.
219 BASIS::Sub::Install::install_installers('Code::Builder'); # just for us, please
220 Code::Builder->install_sub({ name => $code_ref });
222 BASIS::Sub::Install::install_installers('UNIVERSAL'); # feeling lucky, punk?
223 Anything::At::All->install_sub({ name => $code_ref });
225 The installed installers are similar, but not identical, to those provided by
226 BASIS::Sub::Installer. They accept a single hash as an argument. The key/value pairs
227 are used as the C<as> and C<code> parameters to the C<install_sub> routine
228 detailed above. The package name on which the method is called is used as the
231 Unlike BASIS::Sub::Installer's C<install_sub> will not eval strings into code, but
232 will look for named code in the calling package.
236 sub install_installers {
239 for my $method (qw(install_sub reinstall_sub)) {
241 my ($package, $subs) = @_;
242 my ($caller) = caller(0);
244 for (my ($name, $sub) = %$subs) {
245 $return = BASIS::Sub::Install->can($method)->({
254 install_sub({ code => $code, into => $into, as => $method });
260 BASIS::Sub::Install exports C<install_sub> and C<reinstall_sub> only if they are
265 BASIS::Sub::Install has a never-exported subroutine called C<exporter>, which is used
266 to implement its C<import> routine. It takes a hashref of named arguments,
267 only one of which is currently recognize: C<exports>. This must be an arrayref
268 of subroutines to offer for export.
270 This routine is mainly for BASIS::Sub::Install's own consumption. Instead, consider
271 L<BASIS::Sub::Exporter>.
278 my %is_exported = map { $_ => undef } @{ $arg->{exports} };
284 Carp::croak "'$_' is not exported by $class" if !exists $is_exported{$_};
285 install_sub({ code => $_, from => $class, into => $target });
290 BEGIN { *import = exporter({ exports => [ qw(install_sub reinstall_sub) ] }); }
296 =item L<BASIS::Sub::Installer>
298 This module is (obviously) a reaction to Damian Conway's BASIS::Sub::Installer, which
299 does the same thing, but does it by getting its greasy fingers all over
300 UNIVERSAL. I was really happy about the idea of making the installation of
301 coderefs less ugly, but I couldn't bring myself to replace the ugliness of
302 typeglobs and loosened strictures with the ugliness of UNIVERSAL methods.
304 =item L<BASIS::Sub::Exporter>
306 This is a complete Exporter.pm replacement, built atop BASIS::Sub::Install.
312 Ricardo Signes, C<< <rjbs@cpan.org> >>
314 Several of the tests are adapted from tests that shipped with Damian Conway's
315 Sub-Installer distribution.
317 Modified by Andreas Schuh on 6/15/2012 in order to make it a subpackage
318 of the SBIA namespace for inclusion with the BASIS package.
322 Please report any bugs or feature requests through the web interface at
323 L<http://rt.cpan.org>. I will be notified, and then you'll automatically be
324 notified of progress on your bug as I make changes.
328 Copyright 2005-2006 Ricardo Signes, All Rights Reserved.
330 This program is free software; you can redistribute it and/or modify it
331 under the same terms as Perl itself.