1aliased(3) User Contributed Perl Documentation aliased(3)
2
3
4
6 aliased - Use shorter versions of class names.
7
9 # Class name interface
10 use aliased 'My::Company::Namespace::Customer';
11 my $cust = Customer->new;
12
13 use aliased 'My::Company::Namespace::Preferred::Customer' => 'Preferred';
14 my $pref = Preferred->new;
15
16 # Variable interface
17 use aliased;
18 my $Customer = alias "My::Other::Namespace::Customer";
19 my $cust = $Customer->new;
20
21 my $Preferred = alias "My::Other::Namespace::Preferred::Customer";
22 my $pref = $Preferred->new;
23
25 "aliased" is simple in concept but is a rather handy module. It loads
26 the class you specify and exports into your namespace a subroutine that
27 returns the class name. You can explicitly alias the class to another
28 name or, if you prefer, you can do so implicitly. In the latter case,
29 the name of the subroutine is the last part of the class name. Thus,
30 it does something similar to the following:
31
32 #use aliased 'Some::Annoyingly::Long::Module::Name::Customer';
33
34 use Some::Annoyingly::Long::Module::Name::Customer;
35 sub Customer {
36 return 'Some::Annoyingly::Long::Module::Name::Customer';
37 }
38 my $cust = Customer->new;
39
40 This module is useful if you prefer a shorter name for a class. It's
41 also handy if a class has been renamed.
42
43 (Some may object to the term "aliasing" because we're not aliasing one
44 namespace to another, but it's a handy term. Just keep in mind that
45 this is done with a subroutine and not with typeglobs and weird names‐
46 pace munging.)
47
48 Note that this is only for "use"ing OO modules. You cannot use this to
49 load procedural modules. See the Why OO Only? section. Also, don't
50 let the version number fool you. This code is ridiculously simple and
51 is just fine for most use.
52
53 Implicit Aliasing
54
55 The most common use of this module is:
56
57 use aliased 'Some::Module::name';
58
59 "aliased" will allow you to reference the class by the last part of
60 the class name. Thus, "Really::Long::Name" becomes "Name". It does
61 this by exporting a subroutine into your namespace with the same name
62 as the aliased name. This subroutine returns the original class name.
63
64 For example:
65
66 use aliased "Acme::Company::Customer";
67 my $cust = Customer->find($id);
68
69 Note that any class method can be called on the shorter version of the
70 class name, not just the constructor.
71
72 Explicit Aliasing
73
74 Sometimes two class names can cause a conflict (they both end with
75 "Customer" for example), or you already have a subroutine with the same
76 name as the aliased name. In that case, you can make an explicit alias
77 by stating the name you wish to alias to:
78
79 use aliased 'Original::Module::Name' => 'NewName';
80
81 Here's how we use "aliased" to avoid conflicts:
82
83 use aliased "Really::Long::Name";
84 use aliased "Another::Really::Long::Name" => "Aname";
85 my $name = Name->new;
86 my $aname = Aname->new;
87
88 You can even alias to a different package:
89
90 use aliased "Another::Really::Long::Name" => "Another::Name";
91 my $aname = Another::Name->new;
92
93 Messing around with different namespaces is a really bad idea and you
94 probably don't want to do this. However, it might prove handy if the
95 module you are using has been renamed. If the interface has not
96 changed, this allows you to use the new module by only changing one
97 line of code.
98
99 use aliased "New::Module::Name" => "Old::Module::Name";
100 my $thing = Old::Module::Name->new;
101
102 Import Lists
103
104 Sometimes, even with an OO module, you need to specify extra arguments
105 when using the module. When this happens, simply use "Explicit Alias‐
106 ing" followed by the import list:
107
108 Snippet 1:
109
110 use Some::Module::Name qw/foo bar/;
111 my $o = Some::Module::Name->some_class_method;
112
113 Snippet 2 (equivalent to snippet 1):
114
115 use aliased 'Some::Module::Name' => 'Name', qw/foo bar/;
116 my $o = Name->some_class_method;
117
118 Note: remember, you cannot use import lists with "Implicit Aliasing".
119 As a result, you may simply prefer to only use "Explicit Aliasing" as a
120 matter of style.
121
122 alias()
123
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 com‐
150 pile 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
159 Some people have asked why this code only support object-oriented mod‐
160 ules (OO). If I were to support normal subroutines, I would have to
161 allow the following syntax:
162
163 use aliased 'Some::Really::Long::Module::Name';
164 my $data = Name::data();
165
166 That causes a serious problem. The only (reasonable) way it can be
167 done is to handle the aliasing via typeglobs. Thus, instead of a sub‐
168 routine that provides the class name, we alias one package to another
169 (as the namespace module does.) However, we really don't want to sim‐
170 ply alias one package to another and wipe out namespaces willy-nilly.
171 By merely exporting a single subroutine to a namespace, we minimize the
172 issue.
173
174 Fortunately, this doesn't seem to be that much of a problem. Non-OO
175 modules generally support exporting of the functions you need and this
176 eliminates the need for a module such as this.
177
179 This modules exports a subroutine with the same name as the "aliased"
180 name.
181
183 There are no known bugs in this module, but feel free to email me
184 reports.
185
187 The namespace module.
188
190 Many thanks to Rentrak, Inc. (http://www.rentrak.com/) for graciously
191 allowing me to replicate the functionality of some of their internal
192 code.
193
195 Curtis Poe, "ovid [at] cpan [dot] org"
196
198 Copyright (C) 2005 by Curtis "Ovid" Poe
199
200 This library is free software; you can redistribute it and/or modify it
201 under the same terms as Perl itself, either Perl version 5.8.5 or, at
202 your option, any later version of Perl 5 you may have available.
203
204
205
206perl v5.8.8 2007-03-30 aliased(3)