MaTirelire/CGICLI.pm

filter: Statementstmt 6.2% 6 / 96 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 8.3% 1 / 12 Podpod 0.0% 0 / 10 totaltotal 3.4% 7 / 205 CC 41 SCAR 42.4
CC
41
Cov
3%
param
156.0
getCliArgs
90.0
upload
56.0
CRAP 69.2
« MaTirelire/Types.pm ↑ Summary FieldPack.pm »
1 =encoding iso-8859-1
2  
3 =cut
4  
5 #
6 # Copyright (C) 2005, Maxime Soulé
7 # You may distribute this file under the terms of the Artistic
8 # License, as specified in the README file.
9 #
10  
11 package Palm::MaTirelire::CGICLI;
12  
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = '1.0';
16  
17 1 our $AUTOLOAD;
18  
19 #
20 # -cgiopt value will be passed to use CGI qw(XXX)
21 # -cgicode value will be executed just after use CGI; statement
22 # -cginew value will be passed to the CGI new method
23 sub new
24 0 ▶{
Subroutine new: not called CC=5 SCAR=34.0
Pod undocumented
25 0 my $class = shift;
26 0 my $self = {};
27 0 my %args = @_;
28  
29 0 bless $self, $class;
30  
31 # We are in a CGI
32 0 ▶ if ($self->probe_cgi)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->probe_cgi) { } 0 0 0
33 {
34 0 ▶ $args{-cgiopt} = exists($args{-cgiopt}) ? " qw($args{-cgiopt})" : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'-cgiopt'} ? : 0 0 0
35 0 ▶ $args{-cgicode} = '' unless exists $args{-cgicode};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"-cgicode"} 0 0 0
36 0 ▶ $args{-cginew} = exists($args{-cginew}) ? "($args{-cginew})" : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'-cginew'} ? : 0 0 0
37  
38 0 $self->{cgi} = eval <<EOFCGI;
39 use CGI$args{-cgiopt};
40 $args{-cgicode};
41 new CGI$args{-cginew};
42 EOFCGI
43 }
44 # We are on CLI
45 else
46 {
47 0 $self->{cli} = {};
48 0 $self->getCliArgs(@ARGV);
49 }
50  
51 0 return $self;
52 }
53  
54  
55 sub getCliArgs
56 0 ▶{
Subroutine getCliArgs: not called CC=9 SCAR=45.0
Pod undocumented
57 0 my($self, @args) = @_;
58  
59 0 ▶ return if $self->{cgi};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cgi"} 0 0 0
60  
61 0 ▶ for (my $index = 0; $index < @args; $index++)
Branch: 1 branch
BranchTrueFalseTotal
if ($index < @args) 0 0 0
62 {
63 0 my $arg = $args[$index];
64  
65 0 ▶ if ($arg =~ s/^--?//)
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ s/^--?//) { } 0 0 0
66 {
67 # Case -key=value
68 0 ▶ if ($arg =~ /^([^=]+)=(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^([^=]+)=(.*)/) { } 0 0 0
69 {
70 0 $self->{cli}{$1} = $2;
71 }
72 # Case -key value1 value2 value3 OR -boolkey1 -nextopt
73 else
74 {
75 0 my @values;
76  
77 0 for (my $val_idx = $index + 1; ; $val_idx++)
78 {
79 # End of args OR new option
80 0 ▶ if ($val_idx >= @args or $args[$val_idx] =~ /^-/)
Branch: 1 branch
BranchTrueFalseTotal
if ($val_idx >= @args or $args[$val_idx] =~ /^-/) 0 0 0
Condition0%0/3
$val_idx >= @args or $args[$val_idx] =~ /^-/
l!l&&r!l&&!r
000
MC/DC0%0/2
$val_idx >= @args or $args[$val_idx] =~ /^-/
$val_idx >= @args $args[$val_idx] =~ /^-/
Truth table0%0/3
A or B
A= $val_idx >= @argsB= $args[$val_idx] =~ /^-/
ABresult
1X1
011
000
81 {
82 0 $index = $val_idx - 1;
83 0 last;
84 }
85  
86 0 push(@values, $args[$val_idx]);
87 }
88  
89 0 ▶ $self->{cli}{$arg} = @values ? (@values == 1
Branch: 2 branches
BranchTrueFalseTotal
@values ? : 0 0 0
@values == 1 ? : 0 0 0
90 ? $values[0]
91 : \@values) : 1;
92 }
93 }
94 else
95 {
96 0 print STDERR "Unrecognised option: `$arg'\n";
97 }
98 }
99 }
100  
101  
102 sub saveCliArgs
103 0 ▶{
Subroutine saveCliArgs: not called CC=6 SCAR=37.4
Pod undocumented
104 0 my($self, $file) = @_;
105  
106 0 ▶ return if $self->{cgi};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cgi"} 0 0 0
107  
108 0 ▶ open(ARGS, '>', $file) or die "Can't save CLI args: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open ARGS, ">", $file or die "Can't save CLI args: $!\n" 0 0 0
109  
110 0 ▶ while (my($param, $value) = each %{$self->{cli}})
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"cli"};}) 0 0 0
111 {
112 0 print ARGS $param, "\n";
113  
114 0 ▶ if (not defined $value)
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 0 0 0
elsif (ref $value) { } 0 0 0
115 {
116 0 print ARGS "0\n";
117 }
118 elsif (ref $value)
119 {
120 0 print ARGS join("\n", @$value), "\n";
121 }
122 else
123 {
124 0 print ARGS $value, "\n";
125 }
126 }
127  
128 0 close ARGS;
129 }
130  
131  
132 sub loadCliArgs
133 0 ▶{
Subroutine loadCliArgs: not called CC=4 SCAR=30.0
Pod undocumented
134 0 my($self, $file) = @_;
135  
136 0 ▶ return if $self->{cgi};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cgi"} 0 0 0
137  
138 0 my @args;
139  
140 0 ▶ open(ARGS, '<', $file) or die "Can't open CLI args: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open ARGS, "<", $file or die "Can't open CLI args: $!\n" 0 0 0
141  
142 0 ▶ while (defined(my $line = <ARGS>))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline ARGS)) 0 0 0
143 {
144 0 chomp($line);
145 0 push(@args, $line);
146 }
147  
148 0 close ARGS;
149  
150 0 $self->getCliArgs(@args);
151 }
152  
153 #
154 # Used to detect whether we are on CLI or CGI
155 sub probe_cgi
156 0 ▶{
Subroutine probe_cgi: not called CC=1 SCAR=6.9
Pod undocumented
157 0 return exists $ENV{SERVER_NAME};
158 }
159  
160  
161 #
162 # The CGI object, can be used to know in which case we are
163 sub cgi
164 0 ▶{
Subroutine cgi: not called CC=1 SCAR=6.9
Pod undocumented
165 0 return shift->{cgi};
166 }
167  
168  
169 #
170 # Only used on CGI side
171 sub header
172 0 ▶{
Subroutine header: not called CC=2 SCAR=17.9
Pod undocumented
173 0 my $self = shift;
174  
175 0 ▶ if ($self->{cgi})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cgi"}) 0 0 0
176 {
177 0 $self->{cgi}->header(@_);
178 }
179 }
180  
181  
182 sub param
183 0 ▶{
Subroutine param: not called CC=12 SCAR=50.5
Pod undocumented
184 0 my $self = shift;
185  
186 #
187 # CGI
188 #
189 0 ▶ return $self->{cgi}->param(@_) if $self->{cgi};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cgi"} 0 0 0
190  
191 #
192 # CLI
193 #
194  
195 0 ▶ return keys %{$self->{cli}} if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 0 0 0
196  
197 0 my($param_name, $set, $value);
198 0 ▶ if (@_ == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
199 {
200 0 $param_name = $_[0];
201 }
202 else
203 {
204 0 ▶ if ($_[0] =~ /^-(name|value)\z/ and @_ % 2 == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^-(name|value)\z/ and @_ % 2 == 0) { } 0 0 0
Condition0%0/3
$_[0] =~ /^-(name|value)\z/ and @_ % 2 == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] =~ /^-(name|value)\z/ and @_ % 2 == 0
$_[0] =~ /^-(name|value)\z/ @_ % 2 == 0
Truth table0%0/3
A and B
A= $_[0] =~ /^-(name|value)\z/B= @_ % 2 == 0
ABresult
0X0
100
111
205 {
206 0 my %args = @_;
207  
208 0 ▶ if (exists $args{-name})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'-name'}) { } 0 0 0
209 {
210 0 $param_name = $args{-name};
211  
212 0 ▶ if (exists $args{-value})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"-value"}) 0 0 0
213 {
214 0 $set = 1;
215 0 $value = $args{-value};
216 }
217 }
218 else
219 {
220 0 goto normal_way;
221 }
222 }
223 else
224 {
225 0 normal_way:
226 $param_name = shift;
227  
228 0 $set = 1;
229  
230 0 ▶ $value = @_ == 1 ? $_[0] : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
231 }
232 }
233  
234 # SET
235 0 ▶ $self->{cli}{$param_name} = $value if $set;
Branch: 1 branch
BranchTrueFalseTotal
if $set 0 0 0
236  
237 0 ▶ if (wantarray and ref($self->{cli}{$param_name}) eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray and ref $self->{"cli"}{$param_name} eq "ARRAY") 0 0 0
Condition0%0/3
wantarray and ref $self->{"cli"}{$param_name} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
wantarray and ref $self->{"cli"}{$param_name} eq "ARRAY"
wantarray ref $self->{"cli"}{$param_name} eq "ARRAY"
Truth table0%0/3
A and B
A= wantarrayB= ref $self->{"cli"}{$param_name} eq "ARRAY"
ABresult
0X0
100
111
238 {
239 0 return @{$self->{cli}{$param_name}};
240 }
241  
242 0 return $self->{cli}{$param_name};
243 }
244  
245  
246 sub upload
247 0 ▶{
Subroutine upload: not called CC=7 SCAR=40.3
Pod undocumented
248 0 my $self = shift;
249  
250 #
251 # CGI
252 #
253 0 ▶ return $self->{cgi}->upload(@_) if $self->{cgi};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cgi"} 0 0 0
254  
255 #
256 # CLI
257 #
258 0 ▶ my @params = wantarray ? @_ : ($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
259  
260 0 foreach my $param (@params)
261 {
262 0 ▶ if (exists $self->{cli}{$param})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'cli'}{$param}) { } 0 0 0
263 {
264 0 ▶ if (open(my $fh, '<', $self->{cli}{$param}))
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<', $self->{'cli'}{$param}) { } 0 0 0
265 {
266 0 $param = $fh;
267 }
268 else
269 {
270 0 $param = undef;
271 }
272 }
273 else
274 {
275 0 $param = undef;
276 }
277 }
278  
279 0 ▶ return wantarray ? @params : $params[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
280 }
281  
282  
283 sub cgi_error
284 0 ▶{
Subroutine cgi_error: not called CC=2 SCAR=17.9
Pod undocumented
285 0 my $self = shift;
286  
287 0 ▶ return $self->{cgi}->cgi_error if $self->{cgi};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cgi"} 0 0 0
288  
289 # No upload error possible in CLI
290 0 return 0;
291 }
292  
293  
294 sub AUTOLOAD
295 0 ▶{
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
296 0 my $self = shift;
297  
298 #
299 # CGI
300 0 ▶ $self->{cgi}->$AUTOLOAD(@_) if $self->{cgi};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cgi"} 0 0 0
301  
302 #
303 # CLI
304 # Ignore message...
305 }
306  
307 1 1;
308 __END__
« MaTirelire/Types.pm ↑ Summary FieldPack.pm »