Deb wrote:
> 
> Hi,

Hello,

> Here's some (unfinished) code I am trying to use with Getopt::Std,
> 
> #!/usr/local/bin/perl -w
> #
> use Getopt::Std;
> use diagnostics;
> getopts('hn:');
> 
> &usage if (defined $opt_h);
> 
> sub usage { print <<"EOM"
>     USAGE: $0 [-h] -n [EMAIL PROTECTED]
> 
>     -h This message
>     -n <[EMAIL PROTECTED]>
> 
> EOM
> 
> When it's executed, as "test.pl -h" here is what I get in response:
> 
> Name "main::opt_h" used only once: possible typo at test.pl line 16 (#1)
>     (W once) Typographical errors often show up as unique variable names.
>     If you had a good reason for having a unique name, then just mention it
>     again somehow to suppress the message.  The our declaration is
>     provided for this purpose.
> 
>         USAGE: test.pl [-h] -n [EMAIL PROTECTED]
> 
>         -h This message
>         -n <[EMAIL PROTECTED]>
> 
> The program is functional, but strict settings complain, right?

No, you don't have strictures enabled (but you should have.)  You are
getting a warning because you have warnings enabled.  You are using the
$opt_h variable but you haven't declared it or assigned to it or used it
anywhere else.  Getopt::Std creates the $opt_h variable in a different
namespace then your program.


> So what do I do?  I putting "my $opt_h" to initialize the variable, but then that 
> just
> overwrites the setting from the commandline, as you might expect.
> 
> What should I do to rid myself of the complaint?  As far as I can tell, It's
> used only once, and that's all I need.  So, what am I missing?

The best way is to enable strictures and use a lexical hash variable to
hold the options:

#!/usr/local/bin/perl -w
#
use strict;
use Getopt::Std;
use diagnostics;

my %opt;
getopts( 'hn:', \%opt );
usage() if exists $opt{ h };


The next best way is to enable stictures and use package variables:

#!/usr/local/bin/perl -w
#
use strict;
use Getopt::Std;
use diagnostics;

our ( $opt_h, $opt_n );

# on older Perls where our() isn't available
# use vars qw( $opt_h, $opt_n );

getopts( 'hn:' );
usage() if defined $opt_h;


> PS- I'm on the digest...

Is that painful?  BTW I'm on a couch.  :-)



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to