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 / aliased.pm next >
Encoding:
Perl POD Document  |  2009-08-05  |  8.2 KB  |  284 lines

  1. package aliased;
  2. $VERSION = '0.30';
  3.  
  4. require Exporter;
  5. @ISA    = qw(Exporter);
  6. @EXPORT = qw(alias);
  7.  
  8. use strict;
  9.  
  10. sub import {
  11.     my ( $class, $package, $alias, @import ) = @_;
  12.  
  13.     if ( @_ <= 1 ) {
  14.         $class->export_to_level(1);
  15.         return;
  16.     }
  17.  
  18.     my $callpack = caller(0);
  19.  
  20.     _load_alias( $package, $callpack, @import );
  21.     _make_alias( $package, $callpack, $alias );
  22. }
  23.  
  24. sub _get_alias {
  25.     my $package = shift;
  26.     $package =~ s/.*(?:::|')//;
  27.     return $package;
  28. }
  29.  
  30. sub _make_alias {
  31.     my ( $package, $callpack, $alias ) = @_;
  32.  
  33.     $alias ||= _get_alias($package);
  34.  
  35.     no strict 'refs';
  36.     *{ join q{::} => $callpack, $alias } = sub () { $package };
  37. }
  38.  
  39. sub _load_alias {
  40.     my ( $package, $callpack, @import ) = @_;
  41.  
  42.     # We don't localize $SIG{__DIE__} here because we need to be careful about
  43.     # restoring its value if there is a failure.  Very, very tricky.
  44.     my $sigdie = $SIG{__DIE__};
  45.     {
  46.         my $code =
  47.           @import == 0
  48.           ? "package $callpack; use $package;"
  49.           : "package $callpack; use $package (\@import)";
  50.         eval $code;
  51.         if ( my $error = $@ ) {
  52.             $SIG{__DIE__} = $sigdie;
  53.             die $error;
  54.         }
  55.         $sigdie = $SIG{__DIE__}
  56.           if defined $SIG{__DIE__};
  57.     }
  58.  
  59.     # Make sure a global $SIG{__DIE__} makes it out of the localization.
  60.     $SIG{__DIE__} = $sigdie if defined $sigdie;
  61. }
  62.  
  63. sub alias {
  64.     my ( $package, @import ) = @_;
  65.  
  66.     my $callpack = scalar caller(0);
  67.     _load_alias( $package, $callpack, @import );
  68.  
  69.     return $package;
  70. }
  71.  
  72. 1;
  73. __END__
  74.  
  75. =head1 NAME
  76.  
  77. aliased - Use shorter versions of class names.
  78.  
  79. =head1 VERSION
  80.  
  81. 0.30
  82.  
  83. =head1 SYNOPSIS
  84.  
  85.   # Class name interface
  86.   use aliased 'My::Company::Namespace::Customer';
  87.   my $cust = Customer->new;
  88.  
  89.   use aliased 'My::Company::Namespace::Preferred::Customer' => 'Preferred';
  90.   my $pref = Preferred->new;
  91.  
  92.  
  93.   # Variable interface
  94.   use aliased;
  95.   my $Customer  = alias "My::Other::Namespace::Customer";
  96.   my $cust      = $Customer->new;
  97.  
  98.   my $Preferred = alias "My::Other::Namespace::Preferred::Customer";
  99.   my $pref      = $Preferred->new;  
  100.  
  101.  
  102. =head1 DESCRIPTION
  103.  
  104. C<aliased> is simple in concept but is a rather handy module.  It loads the
  105. class you specify and exports into your namespace a subroutine that returns
  106. the class name.  You can explicitly alias the class to another name or, if you
  107. prefer, you can do so implicitly.  In the latter case, the name of the
  108. subroutine is the last part of the class name.  Thus, it does something
  109. similar to the following:
  110.  
  111.   #use aliased 'Some::Annoyingly::Long::Module::Name::Customer';
  112.  
  113.   use Some::Annoyingly::Long::Module::Name::Customer;
  114.   sub Customer {
  115.     return 'Some::Annoyingly::Long::Module::Name::Customer';
  116.   }
  117.   my $cust = Customer->new;
  118.  
  119. This module is useful if you prefer a shorter name for a class.  It's also
  120. handy if a class has been renamed.
  121.  
  122. (Some may object to the term "aliasing" because we're not aliasing one
  123. namespace to another, but it's a handy term.  Just keep in mind that this is
  124. done with a subroutine and not with typeglobs and weird namespace munging.)
  125.  
  126. Note that this is B<only> for C<use>ing OO modules.  You cannot use this to
  127. load procedural modules.  See the L<Why OO Only?|Why OO Only?> section.  Also,
  128. don't let the version number fool you.  This code is ridiculously simple and
  129. is just fine for most use.
  130.  
  131. =head2 Implicit Aliasing
  132.  
  133. The most common use of this module is:
  134.  
  135.   use aliased 'Some::Module::name';
  136.  
  137. C<aliased> will  allow you to reference the class by the last part of the
  138. class name.  Thus, C<Really::Long::Name> becomes C<Name>.  It does this by
  139. exporting a subroutine into your namespace with the same name as the aliased
  140. name.  This subroutine returns the original class name.
  141.  
  142. For example:
  143.  
  144.   use aliased "Acme::Company::Customer";
  145.   my $cust = Customer->find($id);
  146.  
  147. Note that any class method can be called on the shorter version of the class
  148. name, not just the constructor.
  149.  
  150. =head2 Explicit Aliasing
  151.  
  152. Sometimes two class names can cause a conflict (they both end with C<Customer>
  153. for example), or you already have a subroutine with the same name as the
  154. aliased name.  In that case, you can make an explicit alias by stating the
  155. name you wish to alias to:
  156.  
  157.   use aliased 'Original::Module::Name' => 'NewName';
  158.  
  159. Here's how we use C<aliased> to avoid conflicts:
  160.  
  161.   use aliased "Really::Long::Name";
  162.   use aliased "Another::Really::Long::Name" => "Aname";
  163.   my $name  = Name->new;
  164.   my $aname = Aname->new;
  165.  
  166. You can even alias to a different package:
  167.  
  168.   use aliased "Another::Really::Long::Name" => "Another::Name";
  169.   my $aname = Another::Name->new;
  170.  
  171. Messing around with different namespaces is a really bad idea and you probably
  172. don't want to do this.  However, it might prove handy if the module you are
  173. using has been renamed.  If the interface has not changed, this allows you to
  174. use the new module by only changing one line of code.
  175.  
  176.   use aliased "New::Module::Name" => "Old::Module::Name";
  177.   my $thing = Old::Module::Name->new;
  178.  
  179. =head2 Import Lists
  180.  
  181. Sometimes, even with an OO module, you need to specify extra arguments when
  182. using the module.  When this happens, simply use L<Explicit Aliasing> followed
  183. by the import list:
  184.  
  185. Snippet 1:
  186.  
  187.   use Some::Module::Name qw/foo bar/;
  188.   my $o = Some::Module::Name->some_class_method; 
  189.  
  190. Snippet 2 (equivalent to snippet 1):
  191.  
  192.   use aliased 'Some::Module::Name' => 'Name', qw/foo bar/;
  193.   my $o = Name->some_class_method;
  194.  
  195. B<Note>:  remember, you cannot use import lists with L<Implicit Aliasing>.  As
  196. a result, you may simply prefer to only use L<Explicit Aliasing> as a matter
  197. of style.
  198.  
  199. =head2 alias()
  200.  
  201.     my $alias = alias($class);
  202.     my $alias = alias($class, @imports);
  203.  
  204. alias() is an alternative to C<use aliased ...> which uses less magic and
  205. avoids some of the ambiguities.
  206.  
  207. Like C<use aliased> it C<use>s the $class (pass in @imports, if given) but
  208. instead of providing an C<Alias> constant it simply returns a scalar set to
  209. the $class name.
  210.  
  211.     my $thing = alias("Some::Thing::With::A::Long::Name");
  212.  
  213.     # Just like Some::Thing::With::A::Long::Name->method
  214.     $thing->method;
  215.  
  216. The use of a scalar instead of a constant avoids any possible ambiguity
  217. when aliasing two similar names:
  218.  
  219.     # No ambiguity despite the fact that they both end with "Name"
  220.     my $thing = alias("Some::Thing::With::A::Long::Name");
  221.     my $other = alias("Some::Other::Thing::With::A::Long::Name");
  222.  
  223. and there is no magic constant exported into your namespace.
  224.  
  225. The only caveat is loading of the $class happens at run time.  If $class
  226. exports anything you might want to ensure it is loaded at compile time with:
  227.  
  228.     my $thing;
  229.     BEGIN { $thing = alias("Some::Thing"); }
  230.  
  231. However, since OO classes rarely export this should not be necessary.
  232.  
  233.  
  234. =head2 Why OO Only?
  235.  
  236. Some people have asked why this code only support object-oriented modules
  237. (OO).  If I were to support normal subroutines, I would have to allow the
  238. following syntax:
  239.  
  240.   use aliased 'Some::Really::Long::Module::Name';
  241.   my $data = Name::data();
  242.  
  243. That causes a serious problem.  The only (reasonable) way it can be done is to
  244. handle the aliasing via typeglobs.  Thus, instead of a subroutine that
  245. provides the class name, we alias one package to another (as the
  246. L<namespace|namespace> module does.)  However, we really don't want to simply
  247. alias one package to another and wipe out namespaces willy-nilly.  By merely
  248. exporting a single subroutine to a namespace, we minimize the issue. 
  249.  
  250. Fortunately, this doesn't seem to be that much of a problem.  Non-OO modules
  251. generally support exporting of the functions you need and this eliminates the
  252. need for a module such as this.
  253.  
  254. =head1 EXPORT
  255.  
  256. This modules exports a subroutine with the same name as the "aliased" name.
  257.  
  258. =head1 BUGS
  259.  
  260. There are no known bugs in this module, but feel free to email me reports.
  261.  
  262. =head1 SEE ALSO
  263.  
  264. The L<namespace> module.
  265.  
  266. =head1 THANKS
  267.  
  268. Many thanks to Rentrak, Inc. (http://www.rentrak.com/) for graciously allowing
  269. me to replicate the functionality of some of their internal code.
  270.  
  271. =head1 AUTHOR
  272.  
  273. Curtis Poe, C<< ovid [at] cpan [dot] org >>
  274.  
  275. =head1 COPYRIGHT AND LICENSE
  276.  
  277. Copyright (C) 2005 by Curtis "Ovid" Poe
  278.  
  279. This library is free software; you can redistribute it and/or modify
  280. it under the same terms as Perl itself, either Perl version 5.8.5 or,
  281. at your option, any later version of Perl 5 you may have available.
  282.  
  283. =cut
  284.