passing parameters to a perl prgram using getopts

V

vabby

Hi
I have to pass certain parameters to a perl program like if log file is
required or not, wot process is to be setup and the mode. I ahve
written the following script, named bat.pl
use Getopt::Std;

use vars qw($opt_p $opt_m $opt_l);
getopts("p:m:l");
my $process = uc($opt_p) || usage();
my $mode = uc($opt_m) || usage();
my $logging = uc($opt_l) ;
print "\n the value of process is $process\n";
print "\n the value of mode is $mode\n";
print "\n the value of log is $logging\n";
exit(1);

so i invoke the program this way
bat -p Ipc -m qa -l N


my problem is that, whatever the value of the 3rd parameter I pass,
the $logging variable gets the value of 1. This is again true if a
reduce the number of variables to be passed to 2 , then the last
variable again gets the value of 1, irrespective of whatever I pass.
 
A

anno4000

vabby said:
Hi
I have to pass certain parameters to a perl program like if log file is
required or not, wot process is to be setup and the mode. I ahve
written the following script, named bat.pl
use Getopt::Std;

use vars qw($opt_p $opt_m $opt_l);
getopts("p:m:l");

You can avoid the use of package variables if you give getopts()
a lexical hash to work with:

getopts( "p:m:l", \ my %opt);

The option letters and their values become the keys and values of the
hash.
my $process = uc($opt_p) || usage();

So this would become

my $process = uc( $opt{ p}) || usage();

etc...
my $mode = uc($opt_m) || usage();
my $logging = uc($opt_l) ;
print "\n the value of process is $process\n";
print "\n the value of mode is $mode\n";
print "\n the value of log is $logging\n";
exit(1);

so i invoke the program this way
bat -p Ipc -m qa -l N


my problem is that, whatever the value of the 3rd parameter I pass,
the $logging variable gets the value of 1. This is again true if a
reduce the number of variables to be passed to 2 , then the last
variable again gets the value of 1, irrespective of whatever I pass.

Sure. The "l" key doesn't take a value, "p" and "m" do. Re-read perldoc
Getopt::Std to see why.

Anno
 
V

vabby

Was going thru the perldoc, and this is what they had to say:
"For each switch found, sets $opt_x (where x is the switch name) to the
value of the argument if an argument is expected, or 1 otherwise."

I dont understand. can u plz explain, why is the last switch geting the
value of 1.

tx
vabby
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top