RE: shell script problem so simple I'm embarassed to ask

Top Page
Attachments:
Message as email
+ (text/plain)
Delete this message
Reply to this message
Author: der.hans
Date:  
To: plug-discuss
Subject: RE: shell script problem so simple I'm embarassed to ask
Am 11. Feb, 2005 schwätzte Ben Weatherall so:

> Sorry for having to reply from a Gates-based system, it is all I have
> access to at work)...


Well, upgrade $ENV{ 'WORK' } ;-).

> Remember that perl has a nasty feature of automatically instantiating
> any hash entry referenced in a test. In other words, if( $ENV{
> 'USERADMIN_USER' } ) { will cause the USERADMIN_USER entry in the %ENV
> hash to come into being with a null or undefined value, even if it did
> NOT exist prior to the test. In some perl's even the following does not
> keep it from being instantiated, if ( defined($ENV{ 'USERADMIN_USER' } )
> && $ENV{ 'USERADMIN_USER' } ) {


You're talking about auto-vivification ( man pages are nice ), but that
shouldn't affect if() as undef still evaluates to false for if().

You are correct, though, that using defined() is a better practice.

defined() will also auto-vivify.

lufthans@dth:~$ perl -e 'if( $ENV{ 'FRED' } ) { print "fred\n"; } else {
print "nope\n" } if( $ENV{ 'FRED' } ) { print "anke\n"; } else { print
"nope\n"; } if( $ENV{ 'FRED' } ) { print "udo\n"; } else { print "nope\n";
}'
nope
nope
nope
lufthans@dth:~$ perl -e 'if( $ENV{ 'FRED' } ) { print "fred\n"; } else {
print "nope\n" } if( exists( $ENV{ 'FRED' } ) ) { print "anke\n"; } else {
print "nope\n"; } if( defined( $ENV{ 'FRED' } ) ) { print "udo\n"; } else
{ print "nope\n"; }'
nope
nope
nope
lufthans@dth:~$ perl -e 'if( $ENV{ 'FRED' } ) { print "fred\n"; } else {
print "nope\n" } if( defined( $ENV{ 'FRED' } ) ) { print "anke\n"; } else
{ print "nope\n"; } if( exists( $ENV{ 'FRED' } ) ) { print "udo\n"; } else
{ print "nope\n"; }'
nope
nope
nope

> I repeatedly have to keep in mind to do things like, if ( defined($ENV{
> 'USERADMIN_USER' } ) ) { if ( $ENV{ 'USERADMIN_USER' } ) { to keep this
> from happening. Of course, if you only check "true" values then it
> should not make any difference. Also, the environment variables created,
> removed or modified using the %ENV hash are visible to any child process
> spawned by the perl script that modified them.


True, if child processes are dependent on it not existing this could cause
probs.

That also brings up the next level for Craig's code. He's not checking to
see if that variable holds valid data.

ciao,

der.hans
-- 
#  https://www.LuftHans.com/    http://www.AZOTO.org/
#  "The reasons for my decision to quit were myriad, but central to the
#  decision was the realization that there are two kinds of companies:
#  Good ones ask you to think for them.
#  The others tell you to think like them." -- Benjy Feen
---------------------------------------------------
PLUG-discuss mailing list - 
To subscribe, unsubscribe, or to change  you mail settings:
http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss