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

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



-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Guys,

I have this basic perl script:
#!perl -w
use strict;

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?

If you were to re-write this whole section, how would you do it?  This is my thought:

#!perl -w
use strict;

use Env qw(EnvVar1 EnvVar2);

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

Dan

- - - - -
"Wait for that wisest of all counselors, time." -- Pericles
"I do not fear computer, I fear the lack of them." -- Isaac Asimov
GPG fingerprint:6FFD DB94 7B96 0FD8 EADF 2EE0 B2B0 CC47 4FDE 9B68 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFDFx/CsrDMR0/em2gRAjnJAKDjWccSSR95T9C/ZPGSNwMCCQiUzwCfc9Lg TOQ6HWycMlibL+x3LZTBHHs= =okW6 -----END PGP SIGNATURE-----