line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Function::Register; |
2
|
|
|
|
|
|
|
# $Id: Register.pm,v 1.2 2004/05/21 05:18:19 cwest Exp $ |
3
|
1
|
|
|
1
|
|
26527
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
45
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use vars qw[$VERSION %REGISTRIES %REGISTRANTS]; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
348
|
|
6
|
|
|
|
|
|
|
$VERSION = (qw$Revision: 1.2 $)[1]; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Function::Register - Create Function Registries and Register Functions |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
package Company::Employee; |
15
|
|
|
|
|
|
|
use Function::Register; |
16
|
|
|
|
|
|
|
set_register 'Type'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub employee_type { |
19
|
|
|
|
|
|
|
my $self = shift; |
20
|
|
|
|
|
|
|
for ( @Type ) { |
21
|
|
|
|
|
|
|
my $retval = $_->($self); |
22
|
|
|
|
|
|
|
return $retval if $retval; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
return; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# meanwhile, in some other package |
29
|
|
|
|
|
|
|
package Company::Employee::Executive; |
30
|
|
|
|
|
|
|
use Function::Register 'Company::Employee'; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
register Type => \&is_cto; |
33
|
|
|
|
|
|
|
register Type => \&is_ceo; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub is_cto { ... } |
36
|
|
|
|
|
|
|
sub is_ceo { ... } |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# meanwhile, in your program |
39
|
|
|
|
|
|
|
use Company::Employee; |
40
|
|
|
|
|
|
|
use Company::Employee::Executive; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $employee = Company::Employee->new( title => "CEO", ... ); |
43
|
|
|
|
|
|
|
print $employee->employee_type; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 DESCRIPTION |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
This module allows you to declare registers in your namespace, and update |
48
|
|
|
|
|
|
|
registers in other modules. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head2 Exports |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
There are two ways to use this modules. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=over 4 |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=item As the Registry |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
use Function::Register; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
As the registry you simply use the module without any arguments. This will |
61
|
|
|
|
|
|
|
export the C function. It will also create a default register |
62
|
|
|
|
|
|
|
in your namespace called C<@REGISTER>. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=item As the Registrant |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
use Function::Register qw[Some::NameSpace]; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
As the registrant you use the module with a single argument. This will |
69
|
|
|
|
|
|
|
export the C function. It will remember what namespace you |
70
|
|
|
|
|
|
|
want to add to each time you call C. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=back |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub import { |
77
|
2
|
|
|
2
|
|
728
|
my ($class, $registry) = @_; |
78
|
2
|
100
|
|
|
|
7
|
if ( $registry ) { |
79
|
1
|
|
|
|
|
2
|
my $registrant = caller; |
80
|
1
|
|
|
|
|
8
|
$REGISTRANTS{$registrant} = $registry; |
81
|
1
|
|
|
|
|
4
|
_export( $registrant, 'register' ); |
82
|
|
|
|
|
|
|
} else { |
83
|
1
|
|
|
|
|
3
|
_export( scalar(caller()), 'set_register' ); |
84
|
1
|
|
|
|
|
2
|
@_ = (); |
85
|
1
|
|
|
|
|
3
|
goto &set_register; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head2 Functions |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=over 4 |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=item set_registry |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
set_registry 'Name'; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
This function creates a new register in your namespace. A register is a |
98
|
|
|
|
|
|
|
package array of the same name. The call above creates an array, C<@Name>, |
99
|
|
|
|
|
|
|
in your namespace. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=cut |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub set_register($) { |
104
|
3
|
|
100
|
3
|
0
|
4204
|
my $registry = shift() || 'REGISTER'; |
105
|
3
|
|
|
|
|
4
|
my $package = caller; |
106
|
3
|
|
|
|
|
9
|
$REGISTRIES{$package}->{$registry} = 1; |
107
|
3
|
|
|
|
|
3
|
my @reg; |
108
|
1
|
|
|
1
|
|
6
|
no strict 'refs'; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
142
|
|
109
|
3
|
|
|
|
|
3
|
*{"$package\::$registry"} = \@reg; |
|
3
|
|
|
|
|
27
|
|
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=item register |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
register sub { ... }; |
115
|
|
|
|
|
|
|
register Name => \&function_ref; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
This function registeres functions in the namespace you've declared |
118
|
|
|
|
|
|
|
as your registrant. If a single argument is given the function is |
119
|
|
|
|
|
|
|
added to the default registry. If two arguments are given, the first |
120
|
|
|
|
|
|
|
is the name of of the register and the second is a function. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
This function returns a false value if it was unable to add the |
123
|
|
|
|
|
|
|
function to the register. This may be because the register name |
124
|
|
|
|
|
|
|
does not exist, or the function argument isn't a code reference. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
If C is successful it returns true. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
die "Couldn't add to register" |
129
|
|
|
|
|
|
|
unless register \&some_func; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=back |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=cut |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub register($;$) { |
136
|
8
|
|
|
8
|
1
|
846
|
my $reg = 'REGISTER'; |
137
|
8
|
|
|
|
|
16
|
my $func; |
138
|
8
|
100
|
|
|
|
17
|
if ( @_ == 2) { |
139
|
5
|
|
|
|
|
27
|
($reg, $func) = @_; |
140
|
|
|
|
|
|
|
} else { |
141
|
3
|
|
|
|
|
5
|
($func) = @_; |
142
|
|
|
|
|
|
|
} |
143
|
8
|
100
|
|
|
|
25
|
return unless ref($func) eq 'CODE'; |
144
|
7
|
100
|
|
|
|
27
|
return unless exists $REGISTRIES{$REGISTRANTS{caller()}}->{$reg}; |
145
|
6
|
|
|
|
|
11
|
my $registry = join '::', $REGISTRANTS{caller()}, $reg; |
146
|
1
|
|
|
1
|
|
4
|
no strict 'refs'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
68
|
|
147
|
6
|
|
|
|
|
6
|
unshift @{"$registry"}, $func; |
|
6
|
|
|
|
|
29
|
|
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub _export { |
151
|
2
|
|
|
2
|
|
4
|
my ($package, $name) = @_; |
152
|
1
|
|
|
1
|
|
4
|
no strict 'refs'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
62
|
|
153
|
2
|
|
|
|
|
2
|
*{"$package\::$name"} = \&{"$name"}; |
|
2
|
|
|
|
|
20
|
|
|
2
|
|
|
|
|
6
|
|
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
1; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
__END__ |