Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of uninitialized value in string eq at line 38 1

Status
Not open for further replies.

llroberts

Programmer
Oct 7, 2009
2
US
I am VERY new to Perl however I am learning.
I am getting this error from the compiler.

Use of uninitialized value in string eq at /usr/local/lib/perl5/site_perl/5.6.1/GFS/Event.pm line 38.

I know kinda what is causing it... $severity must be null however thats exactly what I am trying to check for.
I want to toss a message telling the user they must provide a severity if it is null. So I guess my issue here is.... I need to check for a null yet the compiler complains about this. How do I fix this?

Here is the section of code it is talking about...

my $severity = $parms{'errorSeverity'} || $parms{'err_severity'};
if ($severity eq '')
{
$this->{'STATUS'} = undef;
$this->{'STATUS_MSG'} = "You must provide a severity using -s in order to send the event. Please try again.";
return;
}


 
That's just warning. It normally would hurt. However, if you want to get rid of that warning message, you can try this:

if ($severity && $severity eq '')

A better way would be like this:

Code:
if (!$severity)
{
  $this->{'STATUS'} = undef;
  $this->{'STATUS_MSG'} = "You must provide a severity using -s in order to send the event.  Please try again.";
  return;
}
 
whn, the line [tt]if ($severity && $severity eq '')[/tt] wouldn't work, because it returns always false (as the string of length 0 is also false).
Also the test [tt]if (!$severity)[/tt] is not fully correct, as if by chance $severity contained a single 0 the test would succeed, but that is likely not the intention of the programmer.
llroberts, your line
[tt]my $severity = $parms{'errorSeverity'} || $parms{'err_severity'};[/tt]
returns undef if neither of the parms is defined. You could write
[tt]my $severity = $parms{'errorSeverity'} || $parms{'err_severity'} || '';[/tt]
and then use [tt]if ($severity eq '')[/tt]
or
use [tt]unless(length$severity)[/tt] with your version.
Note that the concept of a null string or variable is not defined in Perl. A variable may be either undefined (if it has never been set or has positively been set to [tt]undef[/tt]) or contain a value. This value may be either false (the string of length zero or the number 0 and a few more cases) or true, but the undef value is also false.
All this is quite floppy, as perl's authors also recognize, but is a consequence of perl's use of a single type of variable for strings, numbers, references and a few more things.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Thank you for all this great information.
This is the way I fixed it which seems to do the trick....

my $severity = $parms{'errorSeverity'} || "NULL";

if ($severity eq "NULL")
{
$this->{'STATUS'} = undef;
$this->{'STATUS_MSG'} = "You must provide a severity using -s in order to send the event. Please try again.";
return;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top