blib/lib/Getopt/CallingName.pm

filter: Statementstmt 97.4% 38 / 39 Branchbran 83.3% 5 / 6 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 96.7% 59 / 61 CC 6 SCAR 8.3
CC
6
Cov
96%
_get_name
4.0
call_name
3.0
BEGIN
1.0
CRAP 2.3
↑ Summary
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 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 # Standard Perl Library and CPAN modules
54 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use English qw( -no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57  
58 2 our @EXPORT = qw(call_name);
59 2 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 3 ▶sub call_name {
Subroutine call_name: called CC=3 SCAR=11.0
Pod documented
98 3 my(%args) = @_;
99 3 my $name = _get_name(%args);
100 3 ▶ my @args = ($args{args}) ? @{$args{args}} : ();
Branch: 1 branch
BranchTrueFalseTotal
$args{'args'} ? : 3 0 3
101  
102 3 ▶ croak "Unable to call subroutine corresponding to name, &main::$name does not exist" unless(defined &{"main::$name"});
Branch: 1 branch
BranchTrueFalseTotal
unless defined &{"main::$name";} 1 2 3
103
104 {
105 2 package main;
106 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
107 2 return $name->(@args);
108 }
109  
110 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 13 ▶sub _get_name {
Subroutine _get_name: called CC=4 SCAR=13.9
138 13 my(%args) = @_;
139  
140 13 my($name) = $PROGRAM_NAME =~ m!^(?:(?:.*)/)?([^.]*)!;
141 13 ▶ $name =~ s/^$args{name_prefix}// if(defined $args{name_prefix});
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"name_prefix"} 5 8 13
142  
143 13 ▶ $args{method_suffix} ||= '';
Condition100%2/2
$args{"method_suffix"} ||= ""
l!l
112
MC/DC100%1/1
$args{"method_suffix"} ||= ""
$args{"method_suffix"}
Truth table100%2/2
A
A= $args{"method_suffix"}
Aresult
11
00
144 13 ▶ $args{method_prefix} ||= '';
Condition100%2/2
$args{"method_prefix"} ||= ""
l!l
112
MC/DC100%1/1
$args{"method_prefix"} ||= ""
$args{"method_prefix"}
Truth table100%2/2
A
A= $args{"method_prefix"}
Aresult
11
00
145  
146 13 $name = $args{method_prefix} . $name . $args{method_suffix};
147  
148 13 return $name;
149 }
150  
151 2 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<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Getopt-CallingName>
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
↑ Summary