line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Method::Alias; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=pod |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Method::Alias - Create method aliases (and do it safely) |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# My method |
12
|
|
|
|
|
|
|
sub foo { |
13
|
|
|
|
|
|
|
... |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# Alias the method |
17
|
|
|
|
|
|
|
use Method::Alias 'bar' => 'foo', |
18
|
|
|
|
|
|
|
'baz' => 'foo'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 DESCRIPTION |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
For a very long time, whenever I wanted to have a method alias (provide |
23
|
|
|
|
|
|
|
an alternate name for a method) I would simple do a GLOB alias. That is, |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# My method |
26
|
|
|
|
|
|
|
sub foo { |
27
|
|
|
|
|
|
|
... |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Alias the method |
31
|
|
|
|
|
|
|
*bar = *foo; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
While this works fine for functions, it does B work for methods. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
If your class has a subclass that redefines C, any call to C |
36
|
|
|
|
|
|
|
will result in the overloaded method being ignored and the wrong C |
37
|
|
|
|
|
|
|
method being called. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
These are basically bugs waiting to happen, and having completed a number |
40
|
|
|
|
|
|
|
of very large APIs with lots of depth myself, I've been bitten several |
41
|
|
|
|
|
|
|
times. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
In this situation, the canonical and fasest way to handle an alias looks |
44
|
|
|
|
|
|
|
something like this. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# My method |
47
|
|
|
|
|
|
|
sub foo { |
48
|
|
|
|
|
|
|
... |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Alias the method |
52
|
|
|
|
|
|
|
sub bar { shift->foo(@_) } |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Note that this adds an extra entry to the caller array, but this isn't |
55
|
|
|
|
|
|
|
really all that important unless you are paranoid about these things. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
The alternative would be to try to find the method using UNIVERSAL::can, |
58
|
|
|
|
|
|
|
and then goto it. I might add this later if someone really wants it, but |
59
|
|
|
|
|
|
|
until then the basic method will suffice. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
That doing this right is even worthy of a module is debatable, but I |
62
|
|
|
|
|
|
|
would rather have something that looks like a method alias definition, |
63
|
|
|
|
|
|
|
than have to document additional methods all the time. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head2 Using Method::Alias |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Method::Alias is designed to be used as a pragma, to which you provide a |
68
|
|
|
|
|
|
|
set of pairs of method names. Only very minimal checking is done, if you |
69
|
|
|
|
|
|
|
wish to create infinite loops or what have you, you are more than welcome |
70
|
|
|
|
|
|
|
to shoot yourself in the foot. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# Add a single method alias |
73
|
|
|
|
|
|
|
use Method::Alias 'foo' => 'bar'; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# Add several method aliases |
76
|
|
|
|
|
|
|
use Method::Alias 'a' => 'b', |
77
|
|
|
|
|
|
|
'c' => 'd', |
78
|
|
|
|
|
|
|
'e' => 'f'; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
And for now, that's all there is to it. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 METHODS |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=cut |
85
|
|
|
|
|
|
|
|
86
|
2
|
|
|
2
|
|
77064
|
use 5.005; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
90
|
|
87
|
2
|
|
|
2
|
|
14
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
92
|
|
88
|
|
|
|
|
|
|
|
89
|
2
|
|
|
2
|
|
12
|
use vars qw{$VERSION}; |
|
2
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
122
|
|
90
|
|
|
|
|
|
|
BEGIN { |
91
|
2
|
|
|
2
|
|
328
|
$VERSION = '1.03'; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=pod |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head2 import from => to, ... |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Although primarily used as a pragma, you may call import directly if you |
99
|
|
|
|
|
|
|
wish. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Taking a set of pairs of normal strings, the import method creates a number |
102
|
|
|
|
|
|
|
of methods in the caller's package to call the real method. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Returns true, or dies on error. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=cut |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub import { |
109
|
3
|
|
|
3
|
|
493
|
my $class = shift; |
110
|
3
|
|
|
|
|
10
|
my %pairs = @_; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# Where will we create the aliases |
113
|
3
|
|
|
|
|
12
|
my $pkg = (caller())[0]; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# Generate the code |
116
|
4
|
|
|
|
|
19
|
my $code = join "\n", "package $pkg;", |
117
|
3
|
|
|
|
|
16
|
map { "sub $_ { shift->$pairs{$_}(\@_) }" } |
118
|
|
|
|
|
|
|
keys %pairs; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# Execute the code |
121
|
3
|
|
|
4
|
|
235
|
eval $code; |
|
4
|
|
|
1
|
|
1815
|
|
|
1
|
|
|
2
|
|
382
|
|
|
2
|
|
|
1
|
|
827
|
|
|
1
|
|
|
|
|
5
|
|
122
|
3
|
50
|
|
|
|
17
|
die $@ if $@; |
123
|
|
|
|
|
|
|
|
124
|
3
|
|
|
|
|
94
|
1; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
1; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=pod |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 SUPPORT |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Bugs should always be submitted via the CPAN bug tracker |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
L |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
For other issues, contact the maintainer |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 AUTHORS |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Adam Kennedy Ecpan@ali.asE |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 SEE ALSO |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
L |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 COPYRIGHT |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Copyright 2004, 2006 Adam Kennedy. All rights reserved. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
This program is free software; you can redistribute |
152
|
|
|
|
|
|
|
it and/or modify it under the same terms as Perl itself. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
The full text of the license can be found in the |
155
|
|
|
|
|
|
|
LICENSE file included with this module. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=cut |