1aliased(3)            User Contributed Perl Documentation           aliased(3)
2
3
4

NAME

6       aliased - Use shorter versions of class names.
7

VERSION

9       0.30
10

SYNOPSIS

12         # Class name interface
13         use aliased 'My::Company::Namespace::Customer';
14         my $cust = Customer->new;
15
16         use aliased 'My::Company::Namespace::Preferred::Customer' => 'Preferred';
17         my $pref = Preferred->new;
18
19
20         # Variable interface
21         use aliased;
22         my $Customer  = alias "My::Other::Namespace::Customer";
23         my $cust      = $Customer->new;
24
25         my $Preferred = alias "My::Other::Namespace::Preferred::Customer";
26         my $pref      = $Preferred->new;
27

DESCRIPTION

29       "aliased" is simple in concept but is a rather handy module.  It loads
30       the class you specify and exports into your namespace a subroutine that
31       returns the class name.  You can explicitly alias the class to another
32       name or, if you prefer, you can do so implicitly.  In the latter case,
33       the name of the subroutine is the last part of the class name.  Thus,
34       it does something similar to the following:
35
36         #use aliased 'Some::Annoyingly::Long::Module::Name::Customer';
37
38         use Some::Annoyingly::Long::Module::Name::Customer;
39         sub Customer {
40           return 'Some::Annoyingly::Long::Module::Name::Customer';
41         }
42         my $cust = Customer->new;
43
44       This module is useful if you prefer a shorter name for a class.  It's
45       also handy if a class has been renamed.
46
47       (Some may object to the term "aliasing" because we're not aliasing one
48       namespace to another, but it's a handy term.  Just keep in mind that
49       this is done with a subroutine and not with typeglobs and weird
50       namespace munging.)
51
52       Note that this is only for "use"ing OO modules.  You cannot use this to
53       load procedural modules.  See the Why OO Only? section.  Also, don't
54       let the version number fool you.  This code is ridiculously simple and
55       is just fine for most use.
56
57   Implicit Aliasing
58       The most common use of this module is:
59
60         use aliased 'Some::Module::name';
61
62       "aliased" will  allow you to reference the class by the last part of
63       the class name.  Thus, "Really::Long::Name" becomes "Name".  It does
64       this by exporting a subroutine into your namespace with the same name
65       as the aliased name.  This subroutine returns the original class name.
66
67       For example:
68
69         use aliased "Acme::Company::Customer";
70         my $cust = Customer->find($id);
71
72       Note that any class method can be called on the shorter version of the
73       class name, not just the constructor.
74
75   Explicit Aliasing
76       Sometimes two class names can cause a conflict (they both end with
77       "Customer" for example), or you already have a subroutine with the same
78       name as the aliased name.  In that case, you can make an explicit alias
79       by stating the name you wish to alias to:
80
81         use aliased 'Original::Module::Name' => 'NewName';
82
83       Here's how we use "aliased" to avoid conflicts:
84
85         use aliased "Really::Long::Name";
86         use aliased "Another::Really::Long::Name" => "Aname";
87         my $name  = Name->new;
88         my $aname = Aname->new;
89
90       You can even alias to a different package:
91
92         use aliased "Another::Really::Long::Name" => "Another::Name";
93         my $aname = Another::Name->new;
94
95       Messing around with different namespaces is a really bad idea and you
96       probably don't want to do this.  However, it might prove handy if the
97       module you are using has been renamed.  If the interface has not
98       changed, this allows you to use the new module by only changing one
99       line of code.
100
101         use aliased "New::Module::Name" => "Old::Module::Name";
102         my $thing = Old::Module::Name->new;
103
104   Import Lists
105       Sometimes, even with an OO module, you need to specify extra arguments
106       when using the module.  When this happens, simply use "Explicit
107       Aliasing" followed by the import list:
108
109       Snippet 1:
110
111         use Some::Module::Name qw/foo bar/;
112         my $o = Some::Module::Name->some_class_method;
113
114       Snippet 2 (equivalent to snippet 1):
115
116         use aliased 'Some::Module::Name' => 'Name', qw/foo bar/;
117         my $o = Name->some_class_method;
118
119       Note:  remember, you cannot use import lists with "Implicit Aliasing".
120       As a result, you may simply prefer to only use "Explicit Aliasing" as a
121       matter of style.
122
123   alias()
124           my $alias = alias($class);
125           my $alias = alias($class, @imports);
126
127       alias() is an alternative to "use aliased ..." which uses less magic
128       and avoids some of the ambiguities.
129
130       Like "use aliased" it "use"s the $class (pass in @imports, if given)
131       but instead of providing an "Alias" constant it simply returns a scalar
132       set to the $class name.
133
134           my $thing = alias("Some::Thing::With::A::Long::Name");
135
136           # Just like Some::Thing::With::A::Long::Name->method
137           $thing->method;
138
139       The use of a scalar instead of a constant avoids any possible ambiguity
140       when aliasing two similar names:
141
142           # No ambiguity despite the fact that they both end with "Name"
143           my $thing = alias("Some::Thing::With::A::Long::Name");
144           my $other = alias("Some::Other::Thing::With::A::Long::Name");
145
146       and there is no magic constant exported into your namespace.
147
148       The only caveat is loading of the $class happens at run time.  If
149       $class exports anything you might want to ensure it is loaded at
150       compile time with:
151
152           my $thing;
153           BEGIN { $thing = alias("Some::Thing"); }
154
155       However, since OO classes rarely export this should not be necessary.
156
157   Why OO Only?
158       Some people have asked why this code only support object-oriented
159       modules (OO).  If I were to support normal subroutines, I would have to
160       allow the following syntax:
161
162         use aliased 'Some::Really::Long::Module::Name';
163         my $data = Name::data();
164
165       That causes a serious problem.  The only (reasonable) way it can be
166       done is to handle the aliasing via typeglobs.  Thus, instead of a
167       subroutine that provides the class name, we alias one package to
168       another (as the namespace module does.)  However, we really don't want
169       to simply alias one package to another and wipe out namespaces willy-
170       nilly.  By merely exporting a single subroutine to a namespace, we
171       minimize the issue.
172
173       Fortunately, this doesn't seem to be that much of a problem.  Non-OO
174       modules generally support exporting of the functions you need and this
175       eliminates the need for a module such as this.
176

EXPORT

178       This modules exports a subroutine with the same name as the "aliased"
179       name.
180

BUGS

182       There are no known bugs in this module, but feel free to email me
183       reports.
184

SEE ALSO

186       The namespace module.
187

THANKS

189       Many thanks to Rentrak, Inc. (http://www.rentrak.com/) for graciously
190       allowing me to replicate the functionality of some of their internal
191       code.
192

AUTHOR

194       Curtis Poe, "ovid [at] cpan [dot] org"
195
197       Copyright (C) 2005 by Curtis "Ovid" Poe
198
199       This library is free software; you can redistribute it and/or modify it
200       under the same terms as Perl itself, either Perl version 5.8.5 or, at
201       your option, any later version of Perl 5 you may have available.
202
203
204
205perl v5.12.0                      2010-04-29                        aliased(3)
Impressum