For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2007 > Re: Environment variable evaluation in a conditional









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Environment variable evaluation in a conditional
Rob Dixon

2007-09-20, 7:59 am

smdspamcatcher@hotmail.com wrote:
>
> I am currently trying to write a Perl program in a Solaris 9
> environment
> I am trying to process a list of variables with UNIX environment
> variables embedded in them of the form
> $dir_to_check = "$ENV_VAR1/some_dir/$ENV_VAR2/another_dir";
> and I am trying to find if another_dir exists, so I have a line of
> code that tries to do something like this:
>
> if (-e $dir_to_check) { do some stuff }
>
> which is not working even though the directory that I am checking for
> does indeed exist.
>
> Is there something simple that I am just missing, or is there a
> problem with Perl not evaluating the environment
> variables embedded in the path that I am check?


First of all, I think you need to add

use strict;
use warnings;

to the top of your program and declare all Perl variables with 'my'. This
will help you enormously to get your program working.

Also, try printing the value of $dir_to_check to see if it contains the
string you think it does.

Is this your exact code? Because $ENV_VAR1 etc are simply Perl variables
and aren't related to the values of the environment variables. Fortunately
for your programming convenience there is a built-in hash called %ENV which
does contain values from the enviroment. Write something like

my $dir_to_check = "$ENV{varname1}/some_dir/$ENV{varname2}/another_dir";

and you should get the result you expect.

HTH,

Rob
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com