Wednesday, April 27, 2022

[FIXED] Why does my Perl program warn about an uninitialized value?

Issue

I've written the Perl script below which generates a warning and I can't work out why.

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

$status = $ENV{ 'STATUS' };
if ( $status eq "" )
{
    $status = 0;
}
else
{
    $status = 1;
}

It says "Use of uninitialized value in string eq at ./x.pl line 4."

Line 4 is the "if ( $status eq "" )" line, but the variable was initialised...

How can I get rid of this warning?


Solution

$ENV{'STATUS'} may not be defined.

If you run

export STATUS=blah

in the shell before running the Perl script, it will work.

Fix it with

#!/usr/local/bin/perl -w
$status = $ENV{ 'STATUS' };
if (!defined($status) || $status eq "" )
{
    $status = 0;
}
else
{
    $status = 1;
}


Answered By - Charles Ma
Answer Checked By - Willingham (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.