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

Kenneth Thompson kthompson at omnihotels.com
Thu Sep 1 08:47:47 PDT 2005


It could be $EnvVar1 and 2 are in fact not initialized and that is the
complaint.

 

Is this what you're trying to say?

 

my $EnvVar1 = ($EnvVar1 || "DefaultValue1");

my $EnvVar2 = ($EnvVar2 || "DefaultValue2");

my $Var3 = "$EnvVar1 -- $EnvVar2";

 

English:

If for some reason $EnvVar1 doesn't have a value, use the value
"DefaultValue1"

 

 

 

________________________________

From: omaha-pm-bounces at pm.org [mailto:omaha-pm-bounces at pm.org] On Behalf
Of Daniel Linder
Sent: Thursday, September 01, 2005 10:36 AM
To: omaha-pm at pm.org
Subject: [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-----

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/omaha-pm/attachments/20050901/fe4116cc/attachment.html


More information about the Omaha-pm mailing list