aliased.pm 9.4 KB

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