For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2007 > passing parameters to a perl prgram using getopts









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author passing parameters to a perl prgram using getopts
vabby

2007-01-23, 7:58 am

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.

tx
vabby

amphetamachine@gmail.com

2007-01-23, 7:04 pm

Looks like you forgot the colon after the 'l' in the call to getopts().

New line should read:
getopts('p:m:l:');

I would consider using a hash:
my %opts;
getopts('p:m:l:', \%opts);
# and then...
my $logging = $opts{l};
----OR (shorter, but more confusing)----
getopts('p:m:l:', \ my %opts);
# and then still accessing elements as before

Hope this helps

vabby wrote:
> 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.
>
> tx
> vabby


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com