home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / perl5 / Sub / Install.pm < prev   
Encoding:
Perl POD Document  |  2008-02-05  |  8.3 KB  |  333 lines

  1. package Sub::Install;
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. use Carp;
  7. use Scalar::Util ();
  8.  
  9. =head1 NAME
  10.  
  11. Sub::Install - install subroutines into packages easily
  12.  
  13. =head1 VERSION
  14.  
  15. version 0.924
  16.  
  17.  $Id: /my/cs/projects/subinst/trunk/lib/Sub/Install.pm 27898 2006-11-13T15:29:46.377747Z rjbs  $
  18.  
  19. =cut
  20.  
  21. our $VERSION = '0.924';
  22.  
  23. =head1 SYNOPSIS
  24.  
  25.   use Sub::Install;
  26.  
  27.   Sub::Install::install_sub({
  28.     code => sub { ... },
  29.     into => $package,
  30.     as   => $subname
  31.   });
  32.  
  33. =head1 DESCRIPTION
  34.  
  35. This module makes it easy to install subroutines into packages without the
  36. unslightly mess of C<no strict> or typeglobs lying about where just anyone can
  37. see them.
  38.  
  39. =head1 FUNCTIONS
  40.  
  41. =head2 install_sub
  42.  
  43.   Sub::Install::install_sub({
  44.    code => \&subroutine,
  45.    into => "Finance::Shady",
  46.    as   => 'launder',
  47.   });
  48.  
  49. This routine installs a given code reference into a package as a normal
  50. subroutine.  The above is equivalent to:
  51.  
  52.   no strict 'refs';
  53.   *{"Finance::Shady" . '::' . "launder"} = \&subroutine;
  54.  
  55. If C<into> is not given, the sub is installed into the calling package.
  56.  
  57. If C<code> is not a code reference, it is looked for as an existing sub in the
  58. package named in the C<from> parameter.  If C<from> is not given, it will look
  59. in the calling package.
  60.  
  61. If C<as> is not given, and if C<code> is a name, C<as> will default to C<code>.
  62. If C<as> is not given, but if C<code> is a code ref, Sub::Install will try to
  63. find the name of the given code ref and use that as C<as>.
  64.  
  65. That means that this code:
  66.  
  67.   Sub::Install::install_sub({
  68.     code => 'twitch',
  69.     from => 'Person::InPain',
  70.     into => 'Person::Teenager',
  71.     as   => 'dance',
  72.   });
  73.  
  74. is the same as:
  75.  
  76.   package Person::Teenager;
  77.  
  78.   Sub::Install::install_sub({
  79.     code => Person::InPain->can('twitch'),
  80.     as   => 'dance',
  81.   });
  82.  
  83. =head2 reinstall_sub
  84.  
  85. This routine behaves exactly like C<L</install_sub>>, but does not emit a
  86. warning if warnings are on and the destination is already defined.
  87.  
  88. =cut
  89.  
  90. sub _name_of_code {
  91.   my ($code) = @_;
  92.   require B;
  93.   my $name = B::svref_2object($code)->GV->NAME;
  94.   return $name unless $name =~ /\A__ANON__/;
  95.   return;
  96. }
  97.  
  98. # See also Params::Util, to which this code was donated.
  99. sub _CODELIKE {
  100.   (Scalar::Util::reftype($_[0])||'') eq 'CODE'
  101.   || Scalar::Util::blessed($_[0])
  102.   && (overload::Method($_[0],'&{}') ? $_[0] : undef);
  103. }
  104.  
  105. # do the heavy lifting
  106. sub _build_public_installer {
  107.   my ($installer) = @_;
  108.  
  109.   sub {
  110.     my ($arg) = @_;
  111.     my ($calling_pkg) = caller(0);
  112.  
  113.     # I'd rather use ||= but I'm whoring for Devel::Cover.
  114.     for (qw(into from)) { $arg->{$_} = $calling_pkg unless $arg->{$_} }
  115.  
  116.     # This is the only absolutely required argument, in many cases.
  117.     Carp::croak "named argument 'code' is not optional" unless $arg->{code};
  118.  
  119.     if (_CODELIKE($arg->{code})) {
  120.       $arg->{as} ||= _name_of_code($arg->{code});
  121.     } else {
  122.       Carp::croak
  123.         "couldn't find subroutine named $arg->{code} in package $arg->{from}"
  124.         unless my $code = $arg->{from}->can($arg->{code});
  125.  
  126.       $arg->{as}   = $arg->{code} unless $arg->{as};
  127.       $arg->{code} = $code;
  128.     }
  129.  
  130.     Carp::croak "couldn't determine name under which to install subroutine"
  131.       unless $arg->{as};
  132.  
  133.     $installer->(@$arg{qw(into as code) });
  134.   }
  135. }
  136.  
  137. # do the ugly work
  138.  
  139. my $_misc_warn_re;
  140. my $_redef_warn_re;
  141. BEGIN {
  142.   $_misc_warn_re = qr/
  143.     Prototype\ mismatch:\ sub\ .+?  |
  144.     Constant subroutine \S+ redefined
  145.   /x;
  146.   $_redef_warn_re = qr/Subroutine\ \S+\ redefined/x;
  147. }
  148.  
  149. my $eow_re;
  150. BEGIN { $eow_re = qr/ at .+? line \d+\.\Z/ };
  151.  
  152. sub _do_with_warn {
  153.   my ($arg) = @_;
  154.   my $code = delete $arg->{code};
  155.   my $wants_code = sub {
  156.     my $code = shift;
  157.     sub {
  158.       my $warn = $SIG{__WARN__} ? $SIG{__WARN__} : sub { warn @_ }; ## no critic
  159.       local $SIG{__WARN__} = sub {
  160.         my ($error) = @_;
  161.         for (@{ $arg->{suppress} }) {
  162.             return if $error =~ $_;
  163.         }
  164.         for (@{ $arg->{croak} }) {
  165.           if (my ($base_error) = $error =~ /\A($_) $eow_re/x) {
  166.             Carp::croak $base_error;
  167.           }
  168.         }
  169.         for (@{ $arg->{carp} }) {
  170.           if (my ($base_error) = $error =~ /\A($_) $eow_re/x) {
  171.             return $warn->(Carp::shortmess $base_error);
  172.           }
  173.         }
  174.         ($arg->{default} || $warn)->($error);
  175.       };
  176.       $code->(@_);
  177.     };
  178.   };
  179.   return $wants_code->($code) if $code;
  180.   return $wants_code;
  181. }
  182.  
  183. sub _installer {
  184.   sub {
  185.     my ($pkg, $name, $code) = @_;
  186.     no strict 'refs'; ## no critic ProhibitNoStrict
  187.     *{"$pkg\::$name"} = $code;
  188.     return $code;
  189.   }
  190. }
  191.  
  192. BEGIN {
  193.   *_ignore_warnings = _do_with_warn({
  194.     carp => [ $_misc_warn_re, $_redef_warn_re ]
  195.   });
  196.  
  197.   *install_sub = _build_public_installer(_ignore_warnings(_installer));
  198.  
  199.   *_carp_warnings =  _do_with_warn({
  200.     carp     => [ $_misc_warn_re ],
  201.     suppress => [ $_redef_warn_re ],
  202.   });
  203.  
  204.   *reinstall_sub = _build_public_installer(_carp_warnings(_installer));
  205.  
  206.   *_install_fatal = _do_with_warn({
  207.     code     => _installer,
  208.     croak    => [ $_redef_warn_re ],
  209.   });
  210. }
  211.  
  212. =head2 install_installers
  213.  
  214. This routine is provided to allow Sub::Install compatibility with
  215. Sub::Installer.  It installs C<install_sub> and C<reinstall_sub> methods into
  216. the package named by its argument.
  217.  
  218.  Sub::Install::install_installers('Code::Builder'); # just for us, please
  219.  Code::Builder->install_sub({ name => $code_ref });
  220.  
  221.  Sub::Install::install_installers('UNIVERSAL'); # feeling lucky, punk?
  222.  Anything::At::All->install_sub({ name => $code_ref });
  223.  
  224. The installed installers are similar, but not identical, to those provided by
  225. Sub::Installer.  They accept a single hash as an argument.  The key/value pairs
  226. are used as the C<as> and C<code> parameters to the C<install_sub> routine
  227. detailed above.  The package name on which the method is called is used as the
  228. C<into> parameter.
  229.  
  230. Unlike Sub::Installer's C<install_sub> will not eval strings into code, but
  231. will look for named code in the calling package.
  232.  
  233. =cut
  234.  
  235. sub install_installers {
  236.   my ($into) = @_;
  237.  
  238.   for my $method (qw(install_sub reinstall_sub)) {
  239.     my $code = sub {
  240.       my ($package, $subs) = @_;
  241.       my ($caller) = caller(0);
  242.       my $return;
  243.       for (my ($name, $sub) = %$subs) {
  244.         $return = Sub::Install->can($method)->({
  245.           code => $sub,
  246.           from => $caller,
  247.           into => $package,
  248.           as   => $name
  249.         });
  250.       }
  251.       return $return;
  252.     };
  253.     install_sub({ code => $code, into => $into, as => $method });
  254.   }
  255. }
  256.  
  257. =head1 EXPORTS
  258.  
  259. Sub::Install exports C<install_sub> and C<reinstall_sub> only if they are
  260. requested.
  261.  
  262. =head2 exporter
  263.  
  264. Sub::Install has a never-exported subroutine called C<exporter>, which is used
  265. to implement its C<import> routine.  It takes a hashref of named arguments,
  266. only one of which is currently recognize: C<exports>.  This must be an arrayref
  267. of subroutines to offer for export.
  268.  
  269. This routine is mainly for Sub::Install's own consumption.  Instead, consider
  270. L<Sub::Exporter>.
  271.  
  272. =cut
  273.  
  274. sub exporter {
  275.   my ($arg) = @_;
  276.   
  277.   my %is_exported = map { $_ => undef } @{ $arg->{exports} };
  278.  
  279.   sub {
  280.     my $class = shift;
  281.     my $target = caller;
  282.     for (@_) {
  283.       Carp::croak "'$_' is not exported by $class" if !exists $is_exported{$_};
  284.       install_sub({ code => $_, from => $class, into => $target });
  285.     }
  286.   }
  287. }
  288.  
  289. BEGIN { *import = exporter({ exports => [ qw(install_sub reinstall_sub) ] }); }
  290.  
  291. =head1 SEE ALSO
  292.  
  293. =over
  294.  
  295. =item L<Sub::Installer>
  296.  
  297. This module is (obviously) a reaction to Damian Conway's Sub::Installer, which
  298. does the same thing, but does it by getting its greasy fingers all over
  299. UNIVERSAL.  I was really happy about the idea of making the installation of
  300. coderefs less ugly, but I couldn't bring myself to replace the ugliness of
  301. typeglobs and loosened strictures with the ugliness of UNIVERSAL methods.
  302.  
  303. =item L<Sub::Exporter>
  304.  
  305. This is a complete Exporter.pm replacement, built atop Sub::Install.
  306.  
  307. =back
  308.  
  309. =head1 AUTHOR
  310.  
  311. Ricardo Signes, C<< <rjbs@cpan.org> >>
  312.  
  313. Several of the tests are adapted from tests that shipped with Damian Conway's
  314. Sub-Installer distribution.
  315.  
  316. =head1 BUGS
  317.  
  318. Please report any bugs or feature requests to C<bug-sub-install@rt.cpan.org>,
  319. or through the web interface at L<http://rt.cpan.org>.  I will be notified, and
  320. then you'll automatically be notified of progress on your bug as I make
  321. changes.
  322.  
  323. =head1 COPYRIGHT
  324.  
  325. Copyright 2005-2006 Ricardo Signes, All Rights Reserved.
  326.  
  327. This program is free software; you can redistribute it and/or modify it
  328. under the same terms as Perl itself.
  329.  
  330. =cut
  331.  
  332. 1;
  333.