vish has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I was trying to read the file name & processid using the Getopt.

#!/tools/opt/bin/perl -w use Getopt::Std; $iPid = 0; $sFile=""; $Getopt::Std::opt_f = ""; $Getopt::Std::opt_p = 0; getopts('f:p'); if($Getopt::Std::opt_f) {$sFile= $Getopt::Std::opt_f;} if($Getopt::Std::opt_p) {$iPid = $Getopt::Std::opt_p;} print "\n sFile--> $sFile"; print "\n iPID-->$iPid\n";
I will get $sFile as the inputfilename that I give. But why the $iPid will be 1 always when I run the perl script as:
perl <perlfilename> -f <inputfilename> -p <ProcessID>
Can I give the options -f -p in any order? If there are many options also will the order matters? Help me..

Then if I want to read the PID which I provide as an numeric argument along with -p option , the code should be changed as?

Replies are listed 'Best First'.
Re: Getopt in perl
by moritz (Cardinal) on Jan 20, 2009 at 08:07 UTC
    You might be getter off using Getopt::Long, it has a very nice syntax, and is documented very well.

    To answer your questions, as far as I can read them: The order of multiple different options doesn't matter, but some parsers require options to come in front of any arguments.

    That said, I don't see the interface you're using now described in the Getopt::Std documentation - you should stick to the interface described there.

Re: Getopt in perl
by Anonymous Monk on Jan 20, 2009 at 08:03 UTC
    But why the $iPid will be 1 always when I run the perl script
    Because getopts('f:p'); means p is a boolean flag, and f takes an argument.