SPUG: Unitialized values

Joel largest at largest.org
Thu Jun 1 17:59:28 CDT 2000

Peter Darley wrote:

> I have a script that is always throwing up warnings, and I can't for
> the life of me figure out what it's warning me about.  I tried to find
> a description of the error in the perl man pages, but I'm still pretty
> new to it and didn't have any luck.  Anyone have any ideas?  The code
> is as follows, minus its subs (I added the numbering for reference):

> 25 if (param('Action') eq 'GetTableStructure') {
> 26 elsif ((param('Action') eq 'StartImport') || (!param())) {

> 	The errors that I'm getting are:
> Use of uninitialized value at /usr/local/apache/cgi-bin/SetupDB.pl line
> 25.
> Use of uninitialized value at /usr/local/apache/cgi-bin/SetupDB.pl line
> 26.

Since you have warnings turned on, Perl is trying to alert you to anything
that might possibly be a problem.  Here, the culprit is "param('Action')"  
on lines 25 and 26.  It is a potential problem because it is
uninitialized--that is, you're using it to compare to another string
without first checking to see whether it's even defined.  Perl is warning
you that you might be comparing something to an undefined value.  

Anytime you do a comparison (like 'eq') or regex match against a variable
that is undef, you'll get this warning message.  In fact even printing an
undef variable will do it.  For example:

#!/usr/local/bin/perl -w

use strict;

my $foo = 10;
print $foo, "\n";

# no problems so far...

undef $foo;

# but the next line will generate an error since $foo is undef
print $foo, "\n";

# and so will this one
print "Hi Mom!\n" if ( $foo =~ /bar/ );


How do you get around it in your case?  You need to check whether
"param('Action')" is defined before comparing it.  TMTOWTDI, of course...

# first check if "param('Action')" is true...
my $action = param('Action');

if ( $action ) {
  if ( $action eq 'GetTableStructure') {
    print GetTableStructure($dbSystem)
  } elsif (($action eq 'StartImport') || (!param())) {
    print StartImport($dbSystem)
} else {
  print "No <b>Action</b> parameter defined!<p>";

(also, there was a discussion on the merits of using the -w flag recently
on this list. did you see it?)



 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 For Subscriptions, Email to majordomo at pm.org:  ACTION  spug-list  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email address

More information about the spug-list mailing list