TheLinuxDuck
08-07-2002, 01:15 PM
I have a trick I use when passing command line variables that allows me to keep tabs on them, to know what came from the command line, and to establish defaults, if the user doesn't provide anything. It's a very qool system that I came up with when working with CGI's, but it works well for standard params, too:
#!/usr/bin/perl
use strict;
use warnings;
#
# Establish the params we need, and default values
#
my(%params) = (
readDir => { data => "", reqd => "yes", },
writeDir => { data => "", reqd => "yes", },
debug => { data => "no", reqd => "no", },
);
#
# Now, parse command line vars, and put into appropriate hash element
#
for(@ARGV) {
/\=/ or (print "Item '$_' does not contain '='\n" and next);
chomp;
my($key, $value) = split /\=/, $_;
$params{$key}->{data} = $value;
}
#
# Check to make sure items we need filled out were filled out
#
for(keys %params) {
defined($params{$_}->{reqd}) or next;
$params{$_}->{reqd} eq "yes" or next;
$params{$_}->{data} eq "" and die "'$_' required.\n";
}
#
# params are ready! Now do something with them (we're just printing)
#
for(keys %params) {
$params{debug}->{data} eq "yes" and print "memaddy: $params{$_}\n";
print "$_ => $params{$_}->{data}\n";
}
exit;
Handling it like this is a little setup work, but it's really an easy way to handle incoming data.. this could easily be set up in a module, too.
Cheers!
TLD
#!/usr/bin/perl
use strict;
use warnings;
#
# Establish the params we need, and default values
#
my(%params) = (
readDir => { data => "", reqd => "yes", },
writeDir => { data => "", reqd => "yes", },
debug => { data => "no", reqd => "no", },
);
#
# Now, parse command line vars, and put into appropriate hash element
#
for(@ARGV) {
/\=/ or (print "Item '$_' does not contain '='\n" and next);
chomp;
my($key, $value) = split /\=/, $_;
$params{$key}->{data} = $value;
}
#
# Check to make sure items we need filled out were filled out
#
for(keys %params) {
defined($params{$_}->{reqd}) or next;
$params{$_}->{reqd} eq "yes" or next;
$params{$_}->{data} eq "" and die "'$_' required.\n";
}
#
# params are ready! Now do something with them (we're just printing)
#
for(keys %params) {
$params{debug}->{data} eq "yes" and print "memaddy: $params{$_}\n";
print "$_ => $params{$_}->{data}\n";
}
exit;
Handling it like this is a little setup work, but it's really an easy way to handle incoming data.. this could easily be set up in a module, too.
Cheers!
TLD