| 1 | package CGI::Getopt; | ||
| 2 | |||
| 3 | # Perl standard modules | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use CGI::Carp qw(fatalsToBrowser warningsToBrowser); | ||
| 7 | use CGI; | ||
| 8 | use Getopt::Std; | ||
| 9 | use Debug::EchoMessage; | ||
| 10 | |||
| 11 | our $VERSION = 0.13; | ||
| 12 | warningsToBrowser(1); | ||
| 13 | |||
| 14 | require Exporter; | ||
| 15 | our @ISA = qw(Exporter); | ||
| 16 | our @EXPORT = qw(); | ||
| 17 | our @EXPORT_OK = qw(get_inputs read_init_file read_cfg_file | ||
| 18 | ); | ||
| 19 | our %EXPORT_TAGS = ( | ||
| 20 | all => [@EXPORT_OK] | ||
| 21 | ); | ||
| 22 | |||
| 23 | =head1 NAME | ||
| 24 | |||
| 25 | CGI::Getopt - Configuration initializer | ||
| 26 | |||
| 27 | =head1 SYNOPSIS | ||
| 28 | |||
| 29 | use CGI::Getopt; | ||
| 30 | |||
| 31 | my $cg = CGI::Getopt->new('ifn', 'my_init.cfg', 'opt', 'vhS:a:'); | ||
| 32 | my $ar = $cg->get_inputs; | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | This program enables CGI and command line inputs. It uses CGI and | ||
| 37 | Getopt::Std modules. | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | =head3 new (ifn => 'file.cfg', opt => 'hvS:') | ||
| 42 | |||
| 43 | Input variables: | ||
| 44 | |||
| 45 | $ifn - input/initial file name. | ||
| 46 | $opt - options for Getopt::Std | ||
| 47 | |||
| 48 | Variables used or routines called: | ||
| 49 | |||
| 50 | None | ||
| 51 | |||
| 52 | How to use: | ||
| 53 | |||
| 54 | my $cg = new CGI::Getopt; # or | ||
| 55 | my $cg = CGI::Getopt->new; # or | ||
| 56 | my $cg = CGI::Getopt->new(ifn=>'file.cfg',opt=>'hvS:'); # or | ||
| 57 | my $cg = CGI::Getopt->new('ifn', 'file.cfg','opt','hvS:'); | ||
| 58 | |||
| 59 | Return: new empty or initialized CGI::Getopt object. | ||
| 60 | |||
| 61 | This method constructs a Perl object and capture any parameters if | ||
| 62 | specified. It creates and defaults the following variables: | ||
| 63 | |||
| 64 | $self->{ifn} = "" | ||
| 65 | $self->{opt} = 'hvS:'; | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub new { | ||
| 70 | my $caller = shift; | ||
| 71 | my $caller_is_obj = ref($caller); | ||
| 72 | my $class = $caller_is_obj || $caller; | ||
| 73 | my $self = bless {}, $class; | ||
| 74 | my %arg = @_; # convert rest of inputs into hash array | ||
| 75 | foreach my $k ( keys %arg ) { | ||
| 76 | if ($caller_is_obj) { | ||
| 77 | $self->{$k} = $caller->{$k}; | ||
| 78 | } else { | ||
| 79 | $self->{$k} = $arg{$k}; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | $self->{ifn} = "" if ! exists $arg{ifn}; | ||
| 83 | $self->{opt} = 'hvS:' if ! exists $arg{opt}; | ||
| 84 | return $self; | ||
| 85 | } | ||
| 86 | |||
| 87 | =head3 get_inputs($ifn, $opt) | ||
| 88 | |||
| 89 | Input variables: | ||
| 90 | |||
| 91 | $ifn - input/initial file name. | ||
| 92 | $opt - options for Getopt::Std, for instance 'vhS:a:' | ||
| 93 | |||
| 94 | Variables used or routines called: | ||
| 95 | |||
| 96 | None | ||
| 97 | |||
| 98 | How to use: | ||
| 99 | |||
| 100 | my $ar = $self->get_inputs('/tmp/my_init.cfg','vhS:'); | ||
| 101 | |||
| 102 | Return: ($q, $ar) where $q is the CGI object and | ||
| 103 | $ar is a hash array reference containing parameters from web form, | ||
| 104 | or command line and/or configuration file if specified. | ||
| 105 | |||
| 106 | This method performs the following tasks: | ||
| 107 | |||
| 108 | 1) create a CGI object | ||
| 109 | 2) get input from CGI web form or command line | ||
| 110 | 3) read initial file if provided | ||
| 111 | 4) merge the two inputs into one hash array | ||
| 112 | |||
| 113 | This method uses the following rules: | ||
| 114 | |||
| 115 | 1) All parameters in the initial file can not be changed through | ||
| 116 | command line or web form; | ||
| 117 | 2) The "-S" option in command line can be used to set non-single | ||
| 118 | char parameters in the format of | ||
| 119 | -S k1=value1:k2=value2 | ||
| 120 | 3) Single char parameters are included only if they are listed | ||
| 121 | in $opt input variable. | ||
| 122 | |||
| 123 | Some parameters are dfined automatically: | ||
| 124 | |||
| 125 | script_name - $ENV{SCRIPT_NAME} | ||
| 126 | url_dn - $ENV{HTTP_HOST} | ||
| 127 | home_url - http://$ENV{HTTP_HOST} | ||
| 128 | HomeLoc - http://$ENV{HTTP_HOST}/ | ||
| 129 | version - $VERSION | ||
| 130 | action - https://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME} | ||
| 131 | encoding - application/x-www-form-urlencoded | ||
| 132 | method - POST | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | sub get_inputs { | ||
| 137 | my $s = shift; | ||
| 138 | my ($ifn, $par) = @_; | ||
| 139 | |||
| 140 | $ifn = $s->{ifn} if !$ifn; | ||
| 141 | $par = $s->{opt} if !$par; | ||
| 142 | $s->echoMSG("IFN=$ifn\nOPT=$par",3); | ||
| 143 | $s->echoMSG("ARGV: @ARGV",3); | ||
| 144 | |||
| 145 | # return () if (!$ENV{'QUERY_STRING'} && !$ENV{'DOCUMENT_URI'} && | ||
| 146 | # !$ENV{'REMOTE_ADDR'} && !@ARGV); | ||
| 147 | |||
| 148 | my %opt = (); # optional inputs | ||
| 149 | my %cfg = (); # configuration parameters | ||
| 150 | my $ds = '/'; # dir separator | ||
| 151 | my $q = new CGI; # create CGI object | ||
| 152 | my $script_name = ""; | ||
| 153 | $script_name = $ENV{SCRIPT_NAME} if exists $ENV{SCRIPT_NAME}; | ||
| 154 | $opt{script_name} = $script_name; | ||
| 155 | if (exists $ENV{HTTP_HOST}) { | ||
| 156 | $opt{url_dn} = $ENV{HTTP_HOST}; | ||
| 157 | if (exists $ENV{HTTPS} && $ENV{HTTPS} =~ /^on/i) { | ||
| 158 | $opt{action} = "https://$opt{url_dn}$ENV{SCRIPT_NAME}"; | ||
| 159 | } else { | ||
| 160 | $opt{action} = "http://$opt{url_dn}$ENV{SCRIPT_NAME}"; | ||
| 161 | } | ||
| 162 | } else { | ||
| 163 | $opt{url_dn} = ""; | ||
| 164 | $opt{action} = "command line"; | ||
| 165 | } | ||
| 166 | $opt{encoding} = 'application/x-www-form-urlencoded'; | ||
| 167 | $opt{method} = 'POST'; | ||
| 168 | if (!$ENV{'QUERY_STRING'} || @ARGV) { | ||
| 169 | $s->echoMSG("Got ARGV...", 3); | ||
| 170 | # since $s->{opt} was set in new, then we have $par | ||
| 171 | getopts("$par", \%opt); | ||
| 172 | # $s->disp_param(\%opt); | ||
| 173 | } else { | ||
| 174 | $s->echoMSG("Got QUERY_STRING...", 3); | ||
| 175 | # corresponding to ARGV | ||
| 176 | my $p1 = $par; $p1 =~ s/://g; # remove ':" | ||
| 177 | foreach my $k (split //, $p1) { | ||
| 178 | $opt{$k} = $q->param($k); # get inputs | ||
| 179 | } | ||
| 180 | } | ||
| 181 | my @names = (); | ||
| 182 | if (exists $ENV{QUERY_STRING} && $ENV{QUERY_STRING}) { | ||
| 183 | @names = $q->param; | ||
| 184 | } | ||
| 185 | if (exists $opt{S} && $opt{S}) { | ||
| 186 | foreach my $r (split /\:/, $opt{S}) { | ||
| 187 | my ($k, $v) = (split /=/, $r); | ||
| 188 | $opt{$k} = $v if ! exists $opt{$k}; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | foreach my $k (@names) { | ||
| 192 | $opt{$k} = $q->param($k) if ! exists $opt{$k}; | ||
| 193 | } | ||
| 194 | # check input variables | ||
| 195 | $opt{v} = 'n' if ! exists $opt{v} || !defined($opt{v}); | ||
| 196 | $opt{v} = ($opt{v} && $opt{v} =~ /^y/i)?1:$opt{v}; | ||
| 197 | $opt{v} = ($opt{v} =~ /\d+/)?$opt{v}:0; | ||
| 198 | |||
| 199 | %cfg = ($ifn && -f $ifn)?$s->read_init_file($ifn):(); | ||
| 200 | $cfg{version} = "CGI::Getopt $VERSION"; | ||
| 201 | |||
| 202 | if (exists $ENV{HTTP_HOST}) { | ||
| 203 | $cfg{home_url} = "http://$ENV{HTTP_HOST}" if !$cfg{home_url}; | ||
| 204 | $cfg{HomeLoc} = "http://$ENV{HTTP_HOST}/" if !$cfg{HomeLoc}; | ||
| 205 | } else { | ||
| 206 | $cfg{home_url} = "" ; # home URL | ||
| 207 | $cfg{HomeLoc} = "/" ; # ASP var | ||
| 208 | } | ||
| 209 | foreach my $k (keys %opt) { | ||
| 210 | $cfg{$k} = $opt{$k} if ! exists $cfg{$k}; | ||
| 211 | } | ||
| 212 | return ($q, \%cfg); | ||
| 213 | } | ||
| 214 | |||
| 215 | =head3 read_init_file($fn) | ||
| 216 | |||
| 217 | Input variables: | ||
| 218 | |||
| 219 | $fn - full path to a file name | ||
| 220 | |||
| 221 | Variables used or routines called: | ||
| 222 | |||
| 223 | None | ||
| 224 | |||
| 225 | How to use: | ||
| 226 | |||
| 227 | my $ar = $self->read_init_file('crop.cfg'); | ||
| 228 | |||
| 229 | Return: a hash array ref | ||
| 230 | |||
| 231 | This method reads a configuraton file containing parameters in the | ||
| 232 | format of key=values. Multiple lines is allowed for values as long | ||
| 233 | as the lines after the "key=" line are indented as least with two | ||
| 234 | blanks. For instance: | ||
| 235 | |||
| 236 | width = 80 | ||
| 237 | desc = This is a long | ||
| 238 | description about the value | ||
| 239 | |||
| 240 | This will create a hash array of | ||
| 241 | |||
| 242 | ${$ar}{width} = 80 | ||
| 243 | ${$ar}{desc} = "This is a long description about the value" | ||
| 244 | |||
| 245 | =cut | ||
| 246 | |||
| 247 | sub read_init_file { | ||
| 248 | my $s = shift; | ||
| 249 | my ($fn) = @_; | ||
| 250 | if (!$fn) { carp " No file name is specified."; return; } | ||
| 251 | if (!-f $fn) { carp " File - $fn does not exist!"; return; } | ||
| 252 | |||
| 253 | my ($k, $v, %h); | ||
| 254 | open FILE, "< $fn" or | ||
| 255 | croak "ERR: could not read to file - $fn: $!\n"; | ||
| 256 | while (<FILE>) { | ||
| 257 | # skip comment and empty lines | ||
| 258 | next if $_ =~ /^#/ || $_ =~ /^\s*$/; | ||
| 259 | chomp; # remove line break | ||
| 260 | if ($_ =~ /\s*(\w+)\s*=\s*(.+)/) { | ||
| 261 | $k = $1; $v = $2; | ||
| 262 | $v =~ s/\s*#.*$//; # remove inline comments | ||
| 263 | $h{$k} = $v; | ||
| 264 | } else { | ||
| 265 | $v = $_; | ||
| 266 | # remove leading and trailing spaces | ||
| 267 | $v =~ s/^\s+//; $v =~ s/\s+$//; | ||
| 268 | $v =~ s/\s*#.*$//; # remove inline comments | ||
| 269 | $h{$k} .= " $v"; | ||
| 270 | } | ||
| 271 | } | ||
| 272 | close FILE; | ||
| 273 | return %h; | ||
| 274 | } | ||
| 275 | |||
| 276 | =head3 read_cfg_file($fn,$ot, $fs) | ||
| 277 | |||
| 278 | Input variables: | ||
| 279 | |||
| 280 | $fn - full path to a file name | ||
| 281 | $ot - output array type: A(array) or H(hash) | ||
| 282 | $fs - field separator, default to vertical bar (|) | ||
| 283 | |||
| 284 | |||
| 285 | Variables used or routines called: | ||
| 286 | |||
| 287 | echoMSG - display message | ||
| 288 | |||
| 289 | How to use: | ||
| 290 | |||
| 291 | my $arf = $self->read_cfg_file('crop.cfg', 'H'); | ||
| 292 | |||
| 293 | Return: an array or hash array ref containing (${$arf}[$i]{$itm}, | ||
| 294 | ${$arf}[$i][$j]; | ||
| 295 | |||
| 296 | This method reads a configuraton file containing delimited fields. | ||
| 297 | It looks a line starting with '#CN:' for column names. If it finds | ||
| 298 | the line, it uses to define the first row in the array or use the | ||
| 299 | column names as keys in the hash array. | ||
| 300 | |||
| 301 | The default output type is A(array). It will read the field names | ||
| 302 | into the first row ([0][0]~[0][n]). If output array type is hash, | ||
| 303 | then it uses the columns name as keys such as ${$arf}[$i]{key}. | ||
| 304 | If it does not find '#CN:' line, it will use 'FD001' ~ 'FD###' as | ||
| 305 | keys. | ||
| 306 | |||
| 307 | #Form: fm1 | ||
| 308 | #CN: Step|VarName|DispName|Action|Description | ||
| 309 | 0.0|t1|Title||CROP Worksheet | ||
| 310 | |||
| 311 | |||
| 312 | =cut | ||
| 313 | |||
| 314 | sub read_cfg_file { | ||
| 315 | my $s = shift; | ||
| 316 | my ($fn,$ot,$fs) = @_; | ||
| 317 | # | ||
| 318 | if (!$fn) { carp " No file name is specified."; return; } | ||
| 319 | if (!-f $fn) { carp " File - $fn does not exist!"; return; } | ||
| 320 | $ot = 'A' if !$ot; | ||
| 321 | |||
| 322 | my (@a, @b, $i, $j, $k, @keys, $rec); | ||
| 323 | open FILE, "< $fn" or | ||
| 324 | croak "ERR: could not read to file - $fn: $!\n"; | ||
| 325 | @a = <FILE>; | ||
| 326 | close FILE; | ||
| 327 | my @r = (); | ||
| 328 | # get column names first | ||
| 329 | for my $x (0..$#a) { | ||
| 330 | $rec = $a[$x]; | ||
| 331 | next if ($rec !~ /^#\s*CN:\s*(.*)/); | ||
| 332 | $k =$1; $k =~ s/^\s+//; $k =~ s/\s+$//; $k =~ s/\s+/_/g; | ||
| 333 | @keys = split /\|/, $k; | ||
| 334 | $r[0] = [@keys]; | ||
| 335 | last; | ||
| 336 | } | ||
| 337 | |||
| 338 | # get content | ||
| 339 | $i = 0; $rec = ""; | ||
| 340 | for my $x (0..$#a) { | ||
| 341 | # skip comment and empty lines | ||
| 342 | next if ($a[$x] =~ /^#/ || $a[$x] =~ /^\s*$/); | ||
| 343 | chomp $a[$x]; | ||
| 344 | if ($a[$x] =~ /^\s*\|/) { | ||
| 345 | $rec .= " $a[$x]"; # continuous record | ||
| 346 | } elsif (index($a[$x], "\|")>=0) { | ||
| 347 | if ($rec) { # save the previous record | ||
| 348 | $rec =~ s/\s+/ /g; @b = split /\|/, $rec; | ||
| 349 | ++$i; $r[$i]=[@b]; | ||
| 350 | } | ||
| 351 | $rec = $a[$x]; # a new record | ||
| 352 | } else { | ||
| 353 | $rec .= " $a[$x]"; # continuous record | ||
| 354 | } | ||
| 355 | } | ||
| 356 | if ($rec) { | ||
| 357 | $rec =~ s/\s+/ /g; @b = split /\|/, $rec; | ||
| 358 | ++$i; $r[$i]=[@b]; | ||
| 359 | } | ||
| 360 | if (!@keys) { # if it did not get the keys | ||
| 361 | for $j (0..$#{$r[1]}) { | ||
| 362 | push @keys, (sprintf "DF%03d", $j+1); | ||
| 363 | } | ||
| 364 | $r[0] = [@keys]; | ||
| 365 | } | ||
| 366 | return \@r if $ot =~ /^A/i; | ||
| 367 | # | ||
| 368 | my @hr = (); | ||
| 369 | |||
| 370 | # convert the array into hash array | ||
| 371 | for $i (1..$#r) { | ||
| 372 | my %h = (); | ||
| 373 | for $j (0..$#{$r[$i]}) { | ||
| 374 | $k = $r[0][$j]; | ||
| 375 | $h{$k} = $r[$i][$j]; | ||
| 376 | } | ||
| 377 | $j = $i-1; | ||
| 378 | $hr[$j] = \%h; | ||
| 379 | } | ||
| 380 | return \@hr; | ||
| 381 | } | ||
| 382 | |||
| 383 | |||
| 384 | 1; | ||
| 385 | |||
| 386 | =head1 HISTORY | ||
| 387 | |||
| 388 | =over 4 | ||
| 389 | |||
| 390 | =item * Version 0.1 | ||
| 391 | |||
| 392 | This version is to test the concept and routines. | ||
| 393 | |||
| 394 | =item * Version 0.11 | ||
| 395 | |||
| 396 | 04/29/2005 (htu) - fixed a few minor things such as module title. | ||
| 397 | |||
| 398 | =item * Version 0.12 | ||
| 399 | |||
| 400 | Make sure Debug::EchoMessage installed as pre-required. | ||
| 401 | |||
| 402 | =item * Version 0.13 | ||
| 403 | |||
| 404 | Added read_cfg_file routine. | ||
| 405 | |||
| 406 | =cut | ||
| 407 | |||
| 408 | =head1 SEE ALSO (some of docs that I check often) | ||
| 409 | |||
| 410 | Data::Describe, Oracle::Loader, CGI::Getopt, File::Xcopy, | ||
| 411 | perltoot(1), perlobj(1), perlbot(1), perlsub(1), perldata(1), | ||
| 412 | perlsub(1), perlmod(1), perlmodlib(1), perlref(1), perlreftut(1). | ||
| 413 | |||
| 414 | =head1 AUTHOR | ||
| 415 | |||
| 416 | Copyright (c) 2005 Hanming Tu. All rights reserved. | ||
| 417 | |||
| 418 | This package is free software and is provided "as is" without express | ||
| 419 | or implied warranty. It may be used, redistributed and/or modified | ||
| 420 | under the terms of the Perl Artistic License (see | ||
| 421 | http://www.perl.com/perl/misc/Artistic.html) | ||
| 422 | |||
| 423 | =cut | ||
| 424 | |||
| 425 |