This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

env variables

 
D Preethi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic