[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] Proper way to test a variable with strict and -w?




On Sep 1, 2005, at 10:35 AM, Daniel Linder wrote:
 use Env qw(EnvVar1 EnvVar2);

 my $EnvVar1="DefaultValue1" if ("" eq "$EnvVar1");
 my $EnvVar2="DefaultValue2" if ("" eq "$EnvVar2");
 my $Var3 = "$EnvVar1 -- $EnvVar2";

When I run it, the perl interperter complains about "Use of uninitialized value in concatenation" on the "Var3" line (the last line.

Did I mess things up by putting the "my" on the "EnvVar1" and "EnvVar2" lines -- these variables should be defined on the "use Env" line, right?

As I read "perldoc Env" I would say that your use Env line is declaring those two values for you, so your first two my's are redundant. That said, I don't understand why Perl would throw that warning.

 If you were to re-write this whole section, how would you do it?

Assuming 0 is never a legitimate value for those two environment variables, I'd do this:

#!/usr/bin/perl -w

use Env qw(EnvVar1 EnvVar2);

$EnvVar1 ||= "DefaultValue1";
$EnvVar2 ||= "DefaultValue2";
my $Var3 = "$EnvVar1 -- $EnvVar2";

Actually, I wouldn't 'use Env' at all. That's just a "shortcut" for Perl's built-in %ENV. I'd do this:

#!/usr/bin/perl -w

$ENV{Var1} ||= "DefaultValue1";
$ENV{Var2} ||= "DefaultValue2";
my $Var3 = "$ENV{Var1} -- $ENV{Var2}";

HTH,

j