develooper Front page | perl.beginners | Postings from March 2002

Re: Quieting 'Name "main::foo" only used once..."

Thread Previous
From:
Chas Owens
Date:
March 28, 2002 05:02
Subject:
Re: Quieting 'Name "main::foo" only used once..."
Message ID:
1017320328.29566.273.camel@tert.icallinc.com
On Wed, 2002-03-27 at 18:46, eric-perl@pretorious.net wrote:
> Hello, All:
> 
> Is there a simple way to stop perl from complaining that a variable is 
> only used once (possible typo at...)? e.g.,
> 
>   #! /usr/bin/perl -w
>   use Getopts::Std;
> 
>   getopts('d');
> 
>   print "foo" if ($opt_d);
> 
> If I use 'my()' perl complains that the variable is used in a void 
> context. Since I'm testing for truth, I suppose that I could set $opt_d to 
> zero before calling getopts('d')...
> 
> -- 
> Eric P.
> Los Gatos, CA

rewrite the code snippet as

#! /usr/bin/perl -w

use vars '$opt_d'; #tell -w not to be concerned by $opt_d
use Getopts::Std;

getopts('d');

print "foo" if ($opt_d);

or better yet as

#! /usr/bin/perl -w

use vars '$opt_d'; #tell -w not to be concerned by $opt_d
use strict;        #keep me from doing something stupid
use Getopts::Std;

getopts('d');

print "foo" if ($opt_d);


-- 
Today is Boomtime the 14th day of Discord in the YOLD 3168
Keep the Lasagna flying!

Missile Address: 33:48:3.521N  84:23:34.786W


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About