| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Sub::Usage; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Copyright (C) 2002 Trabas. All rights reserved. |
|
4
|
|
|
|
|
|
|
# |
|
5
|
|
|
|
|
|
|
# This program is free software. You may freely use it, modify |
|
6
|
|
|
|
|
|
|
# and/or distribute it under the same term as Perl itself. |
|
7
|
|
|
|
|
|
|
# |
|
8
|
|
|
|
|
|
|
# $Revision: 1.1.1.1 $ |
|
9
|
|
|
|
|
|
|
# $Date: 2002/02/26 00:11:27 $ |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Sub::Usage - Issue subroutine/method usage |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use Sub::Usage; |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub turn_on { |
|
20
|
|
|
|
|
|
|
@_ >= 2 or usage 'NAME, COLOR [, INTENSITY]'; |
|
21
|
|
|
|
|
|
|
# sub continues |
|
22
|
|
|
|
|
|
|
} |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=cut |
|
25
|
|
|
|
|
|
|
|
|
26
|
1
|
|
|
1
|
|
22133
|
use 5.006; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
35
|
|
|
27
|
1
|
|
|
1
|
|
5
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
34
|
|
|
28
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
7
|
|
|
|
1
|
|
|
|
|
33
|
|
|
29
|
1
|
|
|
1
|
|
5
|
use Carp qw(confess cluck); |
|
|
1
|
|
|
|
|
6
|
|
|
|
1
|
|
|
|
|
3153
|
|
|
30
|
|
|
|
|
|
|
require Exporter; |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 EXPORT |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
Only the C function is exported by default. You may optionally |
|
35
|
|
|
|
|
|
|
import the C and C functions or use the tag B<:all> |
|
36
|
|
|
|
|
|
|
to import all available symbols. C will only be imported if it |
|
37
|
|
|
|
|
|
|
is explicitly requested; it is not included in the B<:all> tag. |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=cut |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
|
42
|
|
|
|
|
|
|
our %EXPORT_TAGS = ('all' => [qw(usage warn_hard warn_soft)]); |
|
43
|
|
|
|
|
|
|
our @EXPORT_OK = (@{$EXPORT_TAGS{'all'}}, 'parse_fqpn'); |
|
44
|
|
|
|
|
|
|
our @EXPORT = qw(usage); |
|
45
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _usage { |
|
48
|
4
|
|
|
4
|
|
9
|
my($caller, $arg, $prefix) = @_; |
|
49
|
4
|
50
|
|
|
|
11
|
unless ($caller) { |
|
50
|
0
|
|
|
|
|
0
|
$caller = parse_fqpn((caller 1)[3]); |
|
51
|
0
|
|
|
|
|
0
|
confess __PACKAGE__, |
|
52
|
|
|
|
|
|
|
"::$caller() must be called from a method or subroutine"; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
|
|
55
|
4
|
|
|
|
|
13
|
my $usage = parse_fqpn($caller); |
|
56
|
4
|
100
|
|
|
|
13
|
$usage = "$prefix\->$usage" if defined $prefix; |
|
57
|
4
|
100
|
|
|
|
12
|
$arg = '' unless defined $arg; |
|
58
|
4
|
|
|
|
|
9
|
$usage .= "($arg)"; |
|
59
|
4
|
|
|
|
|
1114
|
return "usage: $usage"; |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 ABSTRACT |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
B provides functions to issue the usage of subroutines |
|
65
|
|
|
|
|
|
|
or methods from inside the stub. The issued usage is part of the |
|
66
|
|
|
|
|
|
|
error message or warning when the subroutine in question is called |
|
67
|
|
|
|
|
|
|
with inappropriate parameters. |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
B provides functions to display usage of subroutines |
|
72
|
|
|
|
|
|
|
or methods from inside the stub. Some people like to check the |
|
73
|
|
|
|
|
|
|
parameters of the routine. For example, |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# turn_on(NAME, COLOR [, INTENSITY]) |
|
76
|
|
|
|
|
|
|
sub turn_on { |
|
77
|
|
|
|
|
|
|
@_ >= 2 or die "usage: turn_on(NAME, COLOR [, INTENSITY])\n"; |
|
78
|
|
|
|
|
|
|
# the process goes on |
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
With the C function (exported by default), you can achieve the same |
|
82
|
|
|
|
|
|
|
results (and more) without having to remember the subroutine name. |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
use Sub::Usage; |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub turn_on { |
|
87
|
|
|
|
|
|
|
@_ >= 2 or usage 'NAME, COLOR [, INTENSITY]'; |
|
88
|
|
|
|
|
|
|
# process goes on |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=over 8 |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=item B |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
The C function makes use of the built-in C function to |
|
96
|
|
|
|
|
|
|
determine the subroutine name. When, for example, C is called |
|
97
|
|
|
|
|
|
|
with inappropriate parameters, C will terminate the program with |
|
98
|
|
|
|
|
|
|
backtrace information and print an error message like the following: |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
usage: turn_on(NAME, COLOR [, INTENSITY]) |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
If C is a method, a prefix can be added to indicate whether |
|
103
|
|
|
|
|
|
|
it is being called as an object method or a class method. |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
package Light::My::Fire; |
|
106
|
|
|
|
|
|
|
use Sub::Usage; |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub turn_on { |
|
110
|
|
|
|
|
|
|
@_ >= 3 or usage 'NAME, COLOR [, INTENSITY]', '$light'; |
|
111
|
|
|
|
|
|
|
# process goes on |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
or, |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub turn_on { |
|
117
|
|
|
|
|
|
|
@_ >= 3 or usage 'NAME, COLOR [, INTENSITY]', __PACKAGE__; |
|
118
|
|
|
|
|
|
|
# process goes on |
|
119
|
|
|
|
|
|
|
} |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
The error message will then be either: |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
usage: $light->turn_on(NAME, COLOR [, INTENSITY]) |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
or, |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
usage: Light::My::Fire->turn_on(NAME, COLOR [, INTENSITY]) |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=cut |
|
130
|
|
|
|
|
|
|
|
|
131
|
2
|
|
|
2
|
1
|
494
|
sub usage { confess _usage((caller 1)[3], @_) } |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=pod |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item B |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=item B |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
The C and C functions are similar to C, but |
|
140
|
|
|
|
|
|
|
they don't die. Instead, as the their names suggest, they only warn |
|
141
|
|
|
|
|
|
|
about the problem and return undef. This can be handy for having the |
|
142
|
|
|
|
|
|
|
subroutine print the error message and return immediately in one |
|
143
|
|
|
|
|
|
|
step. |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub turn_off { |
|
146
|
|
|
|
|
|
|
@_ >= 2 or return warn_hard('NAME', '$light'); |
|
147
|
|
|
|
|
|
|
# process goes on |
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
The difference between the two is that C only works when |
|
151
|
|
|
|
|
|
|
B<$^W> holds true, while C always works regardless of the |
|
152
|
|
|
|
|
|
|
value of B<$^W>. |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=cut |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub warn_hard { |
|
157
|
1
|
|
|
1
|
1
|
95
|
cluck _usage((caller 1)[3], @_); |
|
158
|
1
|
|
|
|
|
6
|
return; |
|
159
|
|
|
|
|
|
|
} |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub warn_soft { |
|
162
|
2
|
100
|
|
2
|
1
|
184
|
cluck _usage((caller 1)[3], @_) if $^W; |
|
163
|
2
|
|
|
|
|
10
|
return; |
|
164
|
|
|
|
|
|
|
} |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=pod |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item B |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
The C function is called internally. It takes a string that |
|
171
|
|
|
|
|
|
|
contains a fully qualified package name and returns pieces of the name. |
|
172
|
|
|
|
|
|
|
It can also accept numeric parameters that determine what it returns. |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
By default, it will just return the last part of the name, which is the |
|
175
|
|
|
|
|
|
|
subroutine name in this case. Of course it doesn't know whether it's |
|
176
|
|
|
|
|
|
|
really a subroutine name or another name from the symbol table, or even |
|
177
|
|
|
|
|
|
|
just garbage. |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
# get subroutine name: usage() |
|
180
|
|
|
|
|
|
|
my $sub = parse_fqpn('Sub::Usage::usage'); |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
# get the package name: Sub::Usage |
|
183
|
|
|
|
|
|
|
my $sub = parse_fqpn('Sub::Usage::usage', 1); |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
# get both the package and sub name |
|
186
|
|
|
|
|
|
|
my($pack, $sub) = parse_fqpn('Sub::Usage::usage', 2); |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
# get all pieces |
|
189
|
|
|
|
|
|
|
my(@parts) = parse_fqpn('Sub::Usage::usage', 3); |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=cut |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
sub parse_fqpn { |
|
194
|
6
|
|
|
6
|
1
|
294
|
my($sub, $how) = @_; |
|
195
|
6
|
50
|
|
|
|
16
|
confess 'usage: parse_fqpn( FQPN [, HOW] )' unless $sub; |
|
196
|
6
|
|
|
|
|
34
|
$sub =~ /(.*)::(.*)/; |
|
197
|
6
|
100
|
|
|
|
26
|
return $2 unless $how; |
|
198
|
1
|
50
|
|
|
|
6
|
return $1 if $how == 1; |
|
199
|
1
|
50
|
|
|
|
8
|
return($1, $2) if $how == 2; |
|
200
|
0
|
|
|
|
|
|
my @packs = split /::/, $1; |
|
201
|
0
|
|
|
|
|
|
return(@packs, $2); |
|
202
|
|
|
|
|
|
|
} |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=pod |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=back |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=head1 BUGS |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
The C function and friends should not be called from anywhere |
|
211
|
|
|
|
|
|
|
outside subroutines or methods, such as the main space. It will die when |
|
212
|
|
|
|
|
|
|
it detects such situation. For example: |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
#!perl |
|
215
|
|
|
|
|
|
|
usage(); |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
This will result in an error message such as: |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
Sub::Usage::usage() must be called from a method or subroutine |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Unfortunately, the underlying function relies too much on C |
|
222
|
|
|
|
|
|
|
to return the fourth element as subroutine name. But this is not the |
|
223
|
|
|
|
|
|
|
situation in eval context, as documented in C. This |
|
224
|
|
|
|
|
|
|
causes the C and friends behave unexpectedly. |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
The workaround is simply don't call them outside of subroutines or methods. |
|
227
|
|
|
|
|
|
|
This is utility for the subs, after all :-) |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head1 AUTHOR |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
Hasanuddin Tamir Ehasant@trabas.comE |
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=head1 COPYRIGHT |
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
Copyright (C) 2002 Trabas. All rights reserved. |
|
236
|
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
This program is free software. You may freely use it, modify |
|
238
|
|
|
|
|
|
|
and/or distribute it under the same term as Perl itself. |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=head1 THANKS |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
I'd like to thank Matthew Sachs Ematthewg@zevils.comE for his |
|
243
|
|
|
|
|
|
|
patch on the POD and suggestion on renaming to Sub::Usage. |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
L. |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=cut |
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
1; |