Subject: Reg parameters to a perl script
Hi ,
I have a perl script which accepts few predefined parameters and
values for them.
It is like this :
myscript.pl -param1 <value1> -param2 <value2>
Now my requirement is , if value is also has and hyphen "-" then
script should accept it and what ever i give after the -param option
should be taken as a single string .
Now the script is raising exception as unknown parameters . My code is
like below
======
"param=s" => [EMAIL PROTECTED]);
print "Parameters passed are @PARAM\n";
print "paramerts in default ARGV are @ARGV\n";
========
and calling the script as
====================
myscript.pl -noexecute -context MALLIK -param -pkg webring web/hosts
--env
===============
The output is
==============
Unknown option: env
Parameters passed are -pkg
paramerts in default ARGV are webring web/hosts
==========================
Here is one way ...
#! /usr/local/bin/perl -w
use strict;
my %param = ('CYCLE'=>'CURRENT','SKIP'=>'0','BAD'=>'NO','DSC'=>'NO');
if (scalar(@ARGV) == 0) {
print 'No options requested, scheduled daily event', "\n";
}
else {
print 'Options requested, other than scheduled event', "\n";
foreach (@ARGV) {
my ($keywd,$value) = split(/=/,uc($_));
if (exists($param{$keywd})) {
$param{$keywd} = $value;
}
else {
print "Option $_ is NOT supported\n";
# die "Option $_ is NOT supported";
}
}
}
print "$_ = $param{$_}\n" foreach (keys(%param));
There is also a "formal" or perl functions to process parameters...
Please see Getopt::Long and Getopt::Std
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>