line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package ExportTo; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
148467
|
use Carp(); |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
66
|
|
4
|
3
|
|
|
3
|
|
16
|
use strict; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
166
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub import{ |
7
|
8
|
|
|
8
|
|
4314
|
my $pkg = (caller)[0]; |
8
|
|
|
|
|
|
|
{ |
9
|
3
|
|
|
3
|
|
15
|
no strict 'refs'; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
907
|
|
|
8
|
|
|
|
|
13
|
|
10
|
6
|
|
|
|
|
51
|
*{$pkg . '::export_to'} = \&export_to |
|
8
|
|
|
|
|
55
|
|
11
|
8
|
100
|
|
|
|
14
|
if not defined &{$pkg . '::export_to'}; |
12
|
|
|
|
|
|
|
} |
13
|
8
|
|
|
|
|
28
|
goto \&export_to; |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub export_to { |
17
|
12
|
100
|
|
12
|
1
|
2560
|
shift if $_[0] eq __PACKAGE__; |
18
|
12
|
|
|
|
|
32
|
my %hash = @_; |
19
|
12
|
|
|
|
|
29
|
my $pkg = (caller)[0]; |
20
|
12
|
|
|
|
|
2121
|
while(my($class, $subs) = each %hash){ |
21
|
12
|
100
|
|
|
|
58
|
if(ref $subs eq 'HASH'){ |
22
|
|
|
|
|
|
|
# {subname => \&coderef/subname} |
23
|
4
|
|
|
|
|
5
|
while (my($sub, $cr_or_name) = each %{$subs}) { |
|
10
|
|
|
|
|
116
|
|
24
|
6
|
100
|
|
|
|
15
|
my($cr, $subname) = ref $cr_or_name eq 'CODE' ? ($cr_or_name, undef) : (undef, $cr_or_name); |
25
|
6
|
|
|
|
|
10
|
my $esub = $class . '::' . $sub; |
26
|
6
|
|
|
|
|
9
|
$sub =~ s/\+//og; |
27
|
6
|
100
|
66
|
|
|
71
|
($esub =~ s/\+//og or ($subname and $subname =~s/\+//og)) ? undef &{$esub} : defined(&{$esub}) && next; |
|
3
|
|
100
|
|
|
9
|
|
28
|
|
|
|
|
|
|
# if($cr or $cr = \&{$pkg . '::' . $subname}) { |
29
|
5
|
50
|
66
|
|
|
37
|
if($cr or $cr = $pkg->can($subname)) { |
30
|
3
|
|
|
3
|
|
16
|
no strict 'refs'; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
693
|
|
31
|
5
|
|
|
|
|
5
|
*{$esub} = $cr |
|
5
|
|
|
|
|
25
|
|
32
|
|
|
|
|
|
|
} else { |
33
|
0
|
|
|
|
|
0
|
Carp::croak($pkg, ' cannot do ' , $subname); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
}else{ |
37
|
8
|
|
|
|
|
14
|
foreach my $sub (@$subs){ |
38
|
13
|
|
|
|
|
15
|
my $esub; |
39
|
13
|
100
|
|
|
|
33
|
unless($sub =~ /::/o){ |
40
|
11
|
|
|
|
|
23
|
$esub = $class . '::' . $sub; |
41
|
|
|
|
|
|
|
} else { |
42
|
2
|
50
|
|
|
|
19
|
$sub =~ s{^(.+)::}{}o and $pkg = $1; |
43
|
2
|
|
|
|
|
6
|
$esub = $class . '::' . $sub; |
44
|
|
|
|
|
|
|
} |
45
|
13
|
|
|
|
|
19
|
$sub =~ s/\+//og; |
46
|
13
|
100
|
50
|
|
|
32
|
$esub =~ s/\+//og ? undef &{$esub} : defined(&{$esub}) && next; |
|
5
|
|
|
|
|
27
|
|
47
|
|
|
|
|
|
|
# if(my $cr = \&{$pkg . '::' . $subname}) { |
48
|
13
|
50
|
|
|
|
101
|
if(my $cr = $pkg->can($sub)) { |
49
|
3
|
|
|
3
|
|
15
|
no strict 'refs'; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
600
|
|
50
|
13
|
|
|
|
|
17
|
*{$esub} = $cr |
|
13
|
|
|
|
|
195
|
|
51
|
|
|
|
|
|
|
} else { |
52
|
0
|
|
|
|
|
|
Carp::croak($pkg, ' cannot do ' , $sub); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head1 NAME |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
ExportTo - export any function/method to any namespace |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 VERSION |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Version 0.03 |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=cut |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 SYNOPSIS |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
package From; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub function_1{ |
76
|
|
|
|
|
|
|
# ... |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub function_2{ |
80
|
|
|
|
|
|
|
# ... |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub function_3{ |
84
|
|
|
|
|
|
|
# ... |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
use ExportTo (NameSpace1 => [qw/function_1 function_2/], NameSpace2 => [qw/function_3/]); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# Now, function_1 and function_2 are exported to 'NameSpace1' namespace. |
90
|
|
|
|
|
|
|
# function_3 is exported to 'NameSpace2' namespace. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# If 'NameSpace1'/'NameSpace2' namespace has same name function/method, |
93
|
|
|
|
|
|
|
# such a function/method is not exported and ExportTo croaks. |
94
|
|
|
|
|
|
|
# but if you want to override, you can do it as following. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
use ExportTo (NameSpace1 => [qw/+function_1 function_2/]); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# if adding + to function/method name, |
99
|
|
|
|
|
|
|
# This override function/method which namespace already has with exported function/method. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
use ExportTo ('+NameSpace' => [qw/function_1 function_2/]); |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# if you add + to namespace name, all functions are exported even if namespace already has function/method. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
use ExportTo ('+NameSpace' => {function_ => sub{print 1}, function_2 => 'function_2'}); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# if using hashref instead of arrayref, its key is regarded as subroutine name and |
108
|
|
|
|
|
|
|
# value is regarded as its coderef/subroutine name. and this subroutine name will be exported. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 DESCRIPTION |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
This module allow you to export/override subroutine/method to one namespace. |
114
|
|
|
|
|
|
|
It can be used for mix-in, for extension of modules not using inheritance. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 FUNCTION/METHOD |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=over 4 |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=item export_to |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# example 1 & 2 |
123
|
|
|
|
|
|
|
export_to(PACKAGE_NAME => [qw/FUNCTION_NAME/]); |
124
|
|
|
|
|
|
|
ExportTo->export_to(PACKAGE_NAME => [qw/FUNCTION_NAME/]); |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# example 3 |
127
|
|
|
|
|
|
|
ExportTo->export_to(PACKAGE_NAME => {SUBROUTINE_NAME => sub{ .... }, SUBROUTINE_NAME2 => 'FUNCTION_NAME'}); |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
These are as same as following. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# example 1 & 2 |
132
|
|
|
|
|
|
|
use ExportTo(PACKAGE_NAME => [qw/FUNCTION_NAME/]); |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# example 3 |
135
|
|
|
|
|
|
|
use ExportTo(PACKAGE_NAME => {SUBROUTINE_NAME => sub{ .... }, SUBROUTINE_NAME2 => 'FUNCTION_NAME'}); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
But, 'use' is needed to declare after declaration of function/method. |
138
|
|
|
|
|
|
|
using 'export_to', you can write anywhere. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=back |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 Export from another package to another package (with renaming). |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
This is used in L. |
145
|
|
|
|
|
|
|
For example, CGI::Util's C function to other package. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
package main; |
148
|
|
|
|
|
|
|
use CGI (); |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# export CGI::Util::escape to OtherA |
151
|
|
|
|
|
|
|
use ExportTo (OtherA => ['CGI::Util::escape']); |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
# export CGI::Util::escape to OtherB as cgi_escape |
154
|
|
|
|
|
|
|
use ExportTo (OtherB => {cgi_escape => \&CGI::Util::escape}); |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
print OtherA::escape("/"); # %2F |
157
|
|
|
|
|
|
|
print OtherB::cgi_escape("/"); # %2F |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 Import from another package's subroutine to current package (with renaming) |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
It is as same as above. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
use CGI (); |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
# export CGI::Util::escape to current package |
166
|
|
|
|
|
|
|
use ExportTo (__PACKAGE__, ['CGI::Util::escape']); |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# export CGI::Util::escape to current package as cgi_escape |
169
|
|
|
|
|
|
|
use ExportTo (__PACKAGE__, {cgi_escape => \&CGI::Util::escape}); |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
print main::escape("/"); # %2F |
172
|
|
|
|
|
|
|
print main::cgi_escape("/"); # %2F |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
But for this purpose, L has better interface. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 AUTHOR |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Ktat, C<< >> |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 BUGS |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
183
|
|
|
|
|
|
|
C, or through the web interface at |
184
|
|
|
|
|
|
|
L. |
185
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on |
186
|
|
|
|
|
|
|
your bug as I make changes. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 SUPPORT |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
perldoc ExportTo |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
You can also look for information at: |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=over 4 |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
L |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=item * CPAN Ratings |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
L |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
L |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=item * Search CPAN |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
L |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=back |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=head1 SEE ALSO |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
L. If you import other module's function to current package, |
219
|
|
|
|
|
|
|
it is better than ExportTo. |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
Copyright 2006-2009 Ktat, all rights reserved. |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
226
|
|
|
|
|
|
|
under the same terms as Perl itself. |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=cut |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
1; # End of ExportTo |