Checking environment variable existence

V

Victor Hannak

Is there an >easy< way to tell if an environment variable exists without
getting a "use of uninitialized variable" error in Perl?

I know I could probably cycle through the entire %ENV array and look for a
match, but I was hoping there was a better way...
 
J

J. Gleixner

Victor said:
Is there an >easy< way to tell if an environment variable exists without
getting a "use of uninitialized variable" error in Perl?

I know I could probably cycle through the entire %ENV array and look for a
match, but I was hoping there was a better way...

You answered your own question. :)

perldoc -f exists
 
A

Andreas Kahari

Is there an >easy< way to tell if an environment variable exists without
getting a "use of uninitialized variable" error in Perl?

I know I could probably cycle through the entire %ENV array and look for a
match, but I was hoping there was a better way...

%ENV is a hash, not an array.


if (exists $ENV{MYVAR} && defined $ENV{MYVAR}) {
# The MYVAR environment variable exists and is not undef
}
 
U

Uri Guttman

AK> if (exists $ENV{MYVAR} && defined $ENV{MYVAR}) { # The MYVAR
AK> environment variable exists and is not undef
AK> }

if the environment is set from the outside and not modified in the perl
script, then you can't get undefined entries. the env in c is a array of
strings of the form key=value so at the minimum you get a value of an
empty string (which is defined). so you can drop the defined (or the
exists) test above in most cases.

uri
 
B

Brian McCauley

Uri Guttman said:
AK> if (exists $ENV{MYVAR} && defined $ENV{MYVAR}) { # The MYVAR
AK> environment variable exists and is not undef
AK> }

if the environment is set from the outside and not modified in the perl
script, then you can't get undefined entries.

Indeed if you are in the habit of using warnings and making sure your
programs don't generate warnings then you can be fairly sure there
won't be any undefs in %ENV even if you _have_ modified the environment
within the Perl script.

And, of course, you can also be sure that for any element of a
non-magical hash (or for a well-behaved magical one like %ENV)
defined() can never be true if exists() is false.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
D

David Efflandt

%ENV is a hash, not an array.


if (exists $ENV{MYVAR} && defined $ENV{MYVAR}) {
# The MYVAR environment variable exists and is not undef
}

If the point is just to do something if it has a value (other than zero),
then 'exists' or 'defined' are not even needed:

#!/usr/bin/perl -w
use strict;
my ($evar) = shift;
if ($ENV{$evar}) {
print "$evar = $ENV{$evar}\n";
} else {
print "$evar is zero or not set\n";
print "however, ".uc($evar)." = $ENV{uc($evar)}\n" if $ENV{uc($evar)};
}
 
M

Malcolm Dew-Jones

Uri Guttman ([email protected]) wrote:

: AK> if (exists $ENV{MYVAR} && defined $ENV{MYVAR}) { # The MYVAR
: AK> environment variable exists and is not undef
: AK> }

: if the environment is set from the outside and not modified in the perl
: script, then you can't get undefined entries.

well only sort of

# try.pl

my $thing = $ENV{'THIS_MIGHT_NOT_BE_DEFINED'} ;

if ($thing == 1)
{
print "$thing\n";
}

C:\> perl -w try.pl
Use of uninitialized value at try.pl line 5.
 
U

Uri Guttman

MD> Uri Guttman ([email protected]) wrote: : >>>>> "AK" == Andreas

MD> : AK> if (exists $ENV{MYVAR} && defined $ENV{MYVAR}) { # The MYVAR
MD> : AK> environment variable exists and is not undef : AK> }

MD> : if the environment is set from the outside and not modified in
^^^^^^^
MD> the perl : script, then you can't get undefined entries.

MD> well only sort of

MD> my $thing = $ENV{'THIS_MIGHT_NOT_BE_DEFINED'} ;

hmm, that looks like code INSIDE the script.

MD> if ($thing == 1) { print "$thing\n";
MD> }

MD> C:\> perl -w try.pl Use of uninitialized value at try.pl line 5.

the point is that if an %ENV entry exists and was set from the outside
it can't be undefined. your code didn't cover that situation.

uri
 
T

Tad McClellan

Malcolm Dew-Jones said:
Uri Guttman ([email protected]) wrote:

: AK> if (exists $ENV{MYVAR} && defined $ENV{MYVAR}) { # The MYVAR
: AK> environment variable exists and is not undef
: AK> }

: if the environment is set from the outside and not modified in the perl
: script, then you can't get undefined entries. ^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^
well only sort of
^^^^^^^

No, exactly.

# try.pl

my $thing = $ENV{'THIS_MIGHT_NOT_BE_DEFINED'} ;


There is no undefined entry in your example.
 
M

Master Web Surfer

[This followup was posted to comp.lang.perl.misc]

Is there an >easy< way to tell if an environment variable exists without
getting a "use of uninitialized variable" error in Perl?

I know I could probably cycle through the entire %ENV array and look for a
match, but I was hoping there was a better way...


if ( exists $ENV{"name_of_var"} ) {
}
 
J

James Willmore

Is there an >easy< way to tell if an environment variable exists
without getting a "use of uninitialized variable" error in Perl?

I know I could probably cycle through the entire %ENV array and look
for a match, but I was hoping there was a better way...

And checking for unset environmental variables, I'm thinking you were
going to need to set any unset values, right?

If so, then this little snipet might help as an example:
#no iteration through %ENV
$ENV{DISPLAY} = 'mydisplay:0.0' unless defined $ENV{DISPLAY};

In this example, you're setting $ENV{DISPLAY} to a value if the
DISPLAY environmental variable is not defined (meaning, it has no
value set). This will work for other values as well.

Of course, you're post wnated to know how to find out if a
environmental variable existed *and* get rid of the "use of ..." error
message. If you set an unset environmental variable, this will get
rid of the message. But you need to know when this occurs (for
example - if the DISPLAY is always unset, then you know to check for
that and set as required and *not* mess with it if it is set). And
you need to know what a sane value to set the unset environmental
variable to. Just puting values in can be dangerious sometimes - at
the very least, break something that's working. Setting the DISPLAY
to some improper value will cause the GUI apps to show up on someone
else's box somewhere else. That's not only rude, but can cause
friction at work :)

All of this makes the presumption that you're going the next step to
set an unset environmental variable. Ignore this if that's not the
case.

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Bugs, pl. n.: Small living things that small living boys throw
on small living girls.
 
A

Anno Siegel

James Willmore said:
And checking for unset environmental variables, I'm thinking you were
^^^^^^^^^^^^^

Ugh. The term is "environment variables". They're not green...

Anno
 
J

James Willmore

On 10 Nov 2003 13:18:10 GMT
^^^^^^^^^^^^^

Ugh. The term is "environment variables". They're not green...

Well, now I've learned something new today.

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Different all twisty a of in maze are you, passages little.
 
M

Matthias Weckman

Purl said:
It is of interest "environment variables" is grammatically incorrect.
Use of "environmental variables" is grammatically correct, both within
the context of the originating author's statement.

Are you sure about that? Isn't "environment variable" really one word?
Like "search engine" or "perl program"? I think it's a compound word.

Or "history professor". I doubt you would write "historic professor" or
"historical professor" unless you were being ironic.
"The environment variables are these."
"Your environment variables are these."

Article Noun Noun...
Possessive Noun Noun...

The search engine is down
Your search engine is flawed
"Environmental variables are these."
"Unset environmental variables here."

Searching engines are here to stay
Debug perly program now to find syntactic errors

;^)
Adjective Noun Verb...
Verb Adjective Noun...

[snip]

TechnoGeeks are not noted for being well versed in language arts;
some are close to being functional illiterates.

I would disagree with this, if only I could write better. "language
arts"? ;^)

[snip]
Technical terminology usage cuts in both directions. My preference
is to use correct grammar to avoid being cut down by those who
believe themselves well armed, and are not. Seems better to cut
some slack for those using correct grammar.

Of course, that is a good way to behave. But I really think
"environmental variable" is not the correct form for "environment
variable". The former would be used in referring to the annual rainfall
or something like that, not your terminal setting (pun intended :).

Matthias
 
M

Matthias Weckman

I suspect the OP's subject line should really read "Checking
environmentally variable existence" then ;-D
 
J

James Willmore

I had to look up _where_ I got "environmental" from. Ironically, page
155 of "Unix Shell Programming" has "environment". So, I guess I
filled in the "al" part :)

Sorry for the fuss - but thanks for pointing out that, for years, I've
been saying it wrong and _nobody_ has corrected me - until now. Or,
if they _have_ corrected me, this is this first time I can say it'll
stick ;-)

Jim
(jwillmore _at_ adelphia _dot_ net)
 
T

Tintin

Matthias Weckman said:
I suspect the OP's subject line should really read "Checking
environmentally variable existence" then ;-D

Nope. Better phrased as "Checking for the existence of environment
variables"
 
A

Anno Siegel

Purl Gurl said:
It is of interest "environment variables" is grammatically incorrect.

Irrelevant. The technical term is "environment variable", and a term
"environmental variable" doesn't exist.

Grammar describes usage, not the other way around. Otherwise, you'd
have to correct everyone who used "impedance". For grammatical reasons
"impedance" is wrong, it should be "impedience". The same goes for a
lot of technical terms, and not only technical terms.

Anno
 
R

Randal L. Schwartz

Purl> It is of interest "environment variables" is grammatically incorrect.

No, doesn't matter. The founding fathers of Unix called them
"environment variables". It's a proper noun. Doesn't matter how
correct or incorrect the english is, any more than the spelling on
"Referer" or "creat()" matters now. It's simply the way it is.

C'mon Kira. You generally pay more attention than that. :)
 
R

Randal L. Schwartz

James> I had to look up _where_ I got "environmental" from. Ironically, page
James> 155 of "Unix Shell Programming" has "environment". So, I guess I
James> filled in the "al" part :)

Which is why I pointed it out. There's a proper term for it, and
you weren't using it. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,888
Messages
2,569,964
Members
46,294
Latest member
HollieYork

Latest Threads

Top