How to avoid warning with CGI.pm method? 
Author Message
 How to avoid warning with CGI.pm method?

I have this script (the actual one is more involved, but this will
exhibit the problem I have):

#!/usr/bin/perl -w
use strict;
use CGI;
my $q=new CGI;
print $q->header;
$q->import_names('WL');

print "We have the answer" if $WL::answer==42;

When I run it, I get a "Name "WL::answer" used only once: possible typo
at /home/nmnadm/weirdloans/cgi-bin/try.pl line 8." warning, which is not
that surprising: at compile time perl has no way of knowing if I'm going
to pass a parameter called "answer" to the script, so it doesn't know
that the import_names method will create a $WL::answer variable.

So, how do I make the warning go away? I could turn warnings off
altogether or I could just idly mention $WL::answer in void context so
it appears in the program a second time, but neither of those seem like
a meaningful solution. Any ideas?



Fri, 02 Dec 2005 15:26:56 GMT  
 How to avoid warning with CGI.pm method?

Quote:

> I have this script (the actual one is more involved, but this will
> exhibit the problem I have):

> #!/usr/bin/perl -w
> use strict;
> use CGI;
> my $q=new CGI;
> print $q->header;
> $q->import_names('WL');

> print "We have the answer" if $WL::answer==42;

> When I run it, I get a "Name "WL::answer" used only once: possible typo
> at /home/nmnadm/weirdloans/cgi-bin/try.pl line 8." warning, which is not
> that surprising: at compile time perl has no way of knowing if I'm going
> to pass a parameter called "answer" to the script, so it doesn't know
> that the import_names method will create a $WL::answer variable.

> So, how do I make the warning go away? I could turn warnings off
> altogether or I could just idly mention $WL::answer in void context so
> it appears in the program a second time, but neither of those seem like
> a meaningful solution. Any ideas?

If you don't need to run the script with Perl versions older than
5.6.0, you can do:

     use warnings;
     no warnings qw(once);

http://www.perldoc.com/perl5.8.0/pod/perllexwarn.html

/ Gunnar

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



Fri, 02 Dec 2005 17:23:48 GMT  
 How to avoid warning with CGI.pm method?
On Mon, 16 Jun 2003 14:26:56 GMT, Andras Malatinszky said:

Quote:
>I have this script (the actual one is more involved, but this will
>exhibit the problem I have):

>#!/usr/bin/perl -w
>use strict;
>use CGI;
>my $q=new CGI;
>print $q->header;
>$q->import_names('WL');

>print "We have the answer" if $WL::answer==42;
> ...
>So, how do I make the warning go away? I could turn warnings off
>altogether or I could just idly mention $WL::answer in void context so
>it appears in the program a second time, but neither of those seem like
>a meaningful solution. Any ideas?

Why not

  my %WL = $q->Vars;
  print "We have the answer" if exists $WL{answer} and $WL{answer}==42;

You don't need the exists() if you always expect an 'answer' parameter
from CGI, because then if you do not in fact get that parameter, the
"Use of uninitialized value in numeric eq" warning is probably
meaningful :-).

--damian



Sat, 03 Dec 2005 01:12:10 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. CGI.pm - warning messages when compiling testscripts

2. CGI.pm Warnings

3. CGI.pm warnings on scrolling_list, radio_group

4. CGI.pm Warnings

5. CGI.pm without warnings?

6. Alias::const: avoiding `used only once...' warning

7. Avoiding Perl warning "uninitialized value"

8. Setuid Scripts...avoiding warnings

9. avoiding "Use of uninitialized value..." warning

10. CGI.pm file_upload method, passing

11. CGI.pm param() method

12. CGI.pm get and post methods exclusive?

 

 
Powered by phpBB® Forum Software