File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Perl and the fly likes env variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Perl
Bookmark "env variables" Watch "env variables" New topic

env variables

D Preethi
Ranch Hand

Joined: May 24, 2008
Posts: 68
Hi All,

Does anyone know how to check if a environment variable is set or not from inside a perl script?

- Preethi
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

Perl has a special associative array created by default, called ENV. So, if you want the PATH, you can do this:

If you want to see a list of all your variable and their values, a loop would look like this:

to see if it's defined or not, just test it:

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

There is an exists() function that can be applied to any associative array. For example to check for the existence of a particular environment variable :-

Just checking the value $ENV{"SOME_NAME"} will not tell you if the environment variable is set since the result will be the same for not set as for it being set but empty. Does the distinction matter to you? If not then just get the value and test it. If so then use exists().

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
I agree. Here's the link:
subject: env variables
It's not a secret anymore!