line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Getopt::CallingName - Script duties delegation based upon calling name |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 SYNOPSIS |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use Getopt::CallingName; |
8
|
|
|
|
|
|
|
call_name( |
9
|
|
|
|
|
|
|
name_prefix => 'tv_', |
10
|
|
|
|
|
|
|
args => \@my_array, |
11
|
|
|
|
|
|
|
); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 DESCRIPTION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Sometimes you can have a script that can run in two or more 'modes' of |
16
|
|
|
|
|
|
|
operation. Apart from an option to specify the mode, the command line options |
17
|
|
|
|
|
|
|
are the same/very similar. Much of the code used by the various modes is common. |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
As an application user interface decision, it may be more useful/helpful to be |
20
|
|
|
|
|
|
|
able to call the script by two or more names - i.e. one for each mode. This cuts |
21
|
|
|
|
|
|
|
out the command line option for specifying the mode. |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
In some cases it might be appropriate just to move all the code, or at least all |
24
|
|
|
|
|
|
|
the common code, into a module and have separate wrapper perl scripts. The |
25
|
|
|
|
|
|
|
problem with this approach is either you end up duplicating command line option |
26
|
|
|
|
|
|
|
handling in each of the wrapper scripts or you end up moving the command line |
27
|
|
|
|
|
|
|
option handling into a module also. The former case introduces maintenance |
28
|
|
|
|
|
|
|
burden [or perhaps an excuse to use TT2 to generate your wrappers ;-)]. The |
29
|
|
|
|
|
|
|
latter case can feel like a distortion/displacement of the code. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Getopt::CallingName provides another alternative. The idea is that you create |
32
|
|
|
|
|
|
|
symbolic links to your actual script (which you might want to give a generic |
33
|
|
|
|
|
|
|
name). Each symbolic link corresponds to the name/mode with which you call the |
34
|
|
|
|
|
|
|
script. Within your script, after any common setup/options handling you call |
35
|
|
|
|
|
|
|
subroutine call_name to call the appropriate script subroutine for the mode. |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head1 PUBLIC INTERFACE |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=cut |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
package Getopt::CallingName; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# pragmata |
48
|
2
|
|
|
2
|
|
53666
|
use 5.006; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
103
|
|
49
|
2
|
|
|
2
|
|
11
|
use base qw(Exporter); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
210
|
|
50
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
62
|
|
51
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
63
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Standard Perl Library and CPAN modules |
54
|
2
|
|
|
2
|
|
10
|
use Carp qw(croak); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
154
|
|
55
|
2
|
|
|
2
|
|
18
|
use English qw( -no_match_vars); |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
14
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
our @EXPORT = qw(call_name); |
59
|
|
|
|
|
|
|
our $VERSION = '1.18'; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# |
62
|
|
|
|
|
|
|
# PUBLIC CLASS METHODS |
63
|
|
|
|
|
|
|
# |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head2 Public Class Methods |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head3 call_name |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
call_name( |
72
|
|
|
|
|
|
|
args => $ra_args, |
73
|
|
|
|
|
|
|
name_prefix => $name_prefix, |
74
|
|
|
|
|
|
|
method_prefix => $method_prefix, |
75
|
|
|
|
|
|
|
method_suffix => $method_suffix, |
76
|
|
|
|
|
|
|
) |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
call_name accepts the following optional arguments: |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
method_prefix - string to prepend to the calculated method name |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
method_suffix - string to append to the calculated method name |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
name_prefix - string to chop off the front of the script name when calculating |
85
|
|
|
|
|
|
|
the method name. Useful if all your modes have a common |
86
|
|
|
|
|
|
|
prefix (tv_record, tv_play ...) |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
args - reference to an array which should be passed to the called sub. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
call_name returns whatever the called subroutine returns. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
call_name checks the subroutine it is going to call to ensure it exists. If it |
93
|
|
|
|
|
|
|
does not exist, call name throws an 'exception' using Carp::croak. |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=cut |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub call_name { |
98
|
3
|
|
|
3
|
1
|
2023
|
my(%args) = @_; |
99
|
3
|
|
|
|
|
12
|
my $name = _get_name(%args); |
100
|
3
|
50
|
|
|
|
8
|
my @args = ($args{args}) ? @{$args{args}} : (); |
|
3
|
|
|
|
|
7
|
|
101
|
|
|
|
|
|
|
|
102
|
3
|
100
|
|
|
|
4
|
croak "Unable to call subroutine corresponding to name, &main::$name does not exist" unless(defined &{"main::$name"}); |
|
3
|
|
|
|
|
211
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
{ |
105
|
2
|
|
|
|
|
2
|
package main; |
106
|
2
|
|
|
2
|
|
1092
|
no strict 'refs'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
438
|
|
107
|
2
|
|
|
|
|
7
|
return $name->(@args); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
0
|
1; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# |
115
|
|
|
|
|
|
|
# PRIVATE CLASS METHODS |
116
|
|
|
|
|
|
|
# |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 INTERNALS |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 Private Class Methods |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head3 _get_name |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
_get_name( |
127
|
|
|
|
|
|
|
name_prefix => $name_prefix, |
128
|
|
|
|
|
|
|
method_prefix => $method_prefix, |
129
|
|
|
|
|
|
|
method_suffix => $method_suffix, |
130
|
|
|
|
|
|
|
) |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Returns the $PROGRAM_NAME after removing any path, prefix (optional) and |
133
|
|
|
|
|
|
|
extension. Adds and optional method prefix and/or suffix as specified. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=cut |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub _get_name { |
138
|
13
|
|
|
13
|
|
1182
|
my(%args) = @_; |
139
|
|
|
|
|
|
|
|
140
|
13
|
|
|
|
|
58
|
my($name) = $PROGRAM_NAME =~ m!^(?:(?:.*)/)?([^.]*)!; |
141
|
13
|
100
|
|
|
|
75
|
$name =~ s/^$args{name_prefix}// if(defined $args{name_prefix}); |
142
|
|
|
|
|
|
|
|
143
|
13
|
|
100
|
|
|
48
|
$args{method_suffix} ||= ''; |
144
|
13
|
|
100
|
|
|
44
|
$args{method_prefix} ||= ''; |
145
|
|
|
|
|
|
|
|
146
|
13
|
|
|
|
|
23
|
$name = $args{method_prefix} . $name . $args{method_suffix}; |
147
|
|
|
|
|
|
|
|
148
|
13
|
|
|
|
|
51
|
return $name; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
1; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 INSTALLATION |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
To install this module type the following: |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
perl Makefile.PL |
158
|
|
|
|
|
|
|
make |
159
|
|
|
|
|
|
|
make test |
160
|
|
|
|
|
|
|
make install |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
This module works only with perl v5.6 and higher. I am more than happy to |
165
|
|
|
|
|
|
|
backport to an earlier perl 5.x if someone using an old perl would like to make |
166
|
|
|
|
|
|
|
use of my module. Mail me and ask me to do the work [or even better do it |
167
|
|
|
|
|
|
|
yourself and send in a patch! ;-)] |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
This module requires these other modules and libraries: |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Carp |
172
|
|
|
|
|
|
|
Test::More |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
The first is required for its operation. The second is for testing purposes |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
This module has these optional dependencies: |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
File::Find::Rule |
179
|
|
|
|
|
|
|
Test::Pod (0.95 or higher) |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
These are both just requried for testing purposes. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head1 POSSIBLE ENHANCEMENTS |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=over 4 |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=item * |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Add a more general method name translation hook |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=back |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head1 BUGS |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
None known at time of writing. To report a bug or request an enhancement use |
196
|
|
|
|
|
|
|
CPAN's excellent Request Tracker: |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
L |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 SOURCE AVAILABILITY |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
This source is part of a SourceForge project which always has the |
203
|
|
|
|
|
|
|
latest sources in svn. |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
http://sourceforge.net/projects/sagar-r-shah/ |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head1 AUTHOR |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Sagar R. Shah |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head1 COPYRIGHT |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Copyright 2003-2007, Sagar R. Shah, All rights reserved |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
216
|
|
|
|
|
|
|
under the same terms as Perl itself. |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=cut |