views:

309

answers:

3

I've written a 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..

Any ideas how I can get rid of this warning..

+5  A: 

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

If you run

export STATUS=blah

in 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;
}
Charles Ma
setting the env var or not doesn't work. I tried that first.
ScaryAardvark
But the defined($status) seems to do the trick, thx.
ScaryAardvark
Why not `if ( defined $status and length $status ) { $status = 1 } else { $status = 0 }`? It makes the condition a bit simpler.
daotoad
+3  A: 

I know your question is about the warning but you can use the conditional operator ?: to set $status if not defined or empty string

$status = $ENV{'STATUS'} ? 1 : 0;
ccheneson
Note that this does not distinguish between `$ENV{STATUS}` not being set and `$ENV{STATUS}` being set to 0. That's usually not an issue, but can cause subtle bugs in some cases.
Dave Sherohman
This is a fairly common newbie bug (and why are people voting it up?). You only want to set a default value if the value isn't already defined. If you're using Perl 5.10, you can use the `$status = $ENV{'STATUS'} // 0`; . Before Perl 5.10, you have to pull out `defined` to check it.
brian d foy
@brian: By "you have to pull out defined to check", do you mean having $status = defined($ENV{'STATUS'}) ? 1 : 0; ?If the $ENV{'STATUS'} is set to empty string (export STATUS=''),defined($ENV{'STATUS'}) will evaluate to true (value has been set) so $status will be set to 1, but $ENV{'STATUS'} will evaluate to false so set $status to 0; The OP wants that, at the end, if there is an empty string, $status = 0. Am I misinterpreting something?
ccheneson
The OP is trying to get rid of a warning. So, you have to get rid of the warning. :)
brian d foy
+7  A: 

Another option is

$status = $ENV{ 'STATUS' } // '';

which will set $status to an empty string if it is not defined, but the // operator only exists in perl 5.10 and later. The equivalent pre-5.10 syntax is

$status = defined $ENV{STATUS} ? $ENV{STATUS} : '';

although a lot of people will fake it with

$status = $ENV{ 'STATUS' } || '';

but this will change an $ENV{STATUS} of 0 into an empty string, which may or may not be an issue for you.

Those are all general-case answers, however. In the particular code you posted, replacing the whole thing with

$status = defined $ENV{STATUS} ? 1 : 0;

or even just

$status = defined $ENV{STATUS};

(if you're OK with the 'not set' value being an empty string instead of 0) would be the better option.

Dave Sherohman