File Coverage

blib/lib/Palm/MaTirelire/CGICLI.pm
Criterion Covered Total %
statement 3 93 3.2
branch 0 66 0.0
condition 0 9 0.0
subroutine 1 12 8.3
pod 0 10 0.0
total 4 190 2.1


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