For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2007 > 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 Environment variable evaluation in a conditional
smdspamcatcher@hotmail.com

2007-09-20, 7:59 am

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?

smdspamcatcher@hotmail.com

2007-09-21, 4:00 am

On Sep 20, 2:54 am, rob.di...@350.com (Rob Dixon) wrote:
> smdspamcatc...@hotmail.com wrote:
>
>
>
>
>
> 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- Hide quoted text -
>
> - Show quoted text -



Rob,

Actually, $ENV1 and $ENV2 are environment variable that are set by
someone else's script. They are embedded
in the strings that I have to process. I'd like to avoid using the
%ENV hash or adding yet more pattern matching to
my program if there is a simpler way of doing things.

It's not my exact code, merely the gist of what I am trying to do. As
I said, the directory I am searching for exists.
If I print out the $dir_to_check string, it gives me
$ENV1/some_dir/$ENV2/another_dir
and echo $ENV1 and echo $ENV2 typed from the shell expand to give me
what I expect. Being somewhat new
to Perl, I am not entirely sure of the behavior of all the commands.
Perl seems to be remarkably intuitive, and I was
hoping that either a file existence check would automatically
interpret a string containing $VAR_NAME as an
environment variable, or that there was some simple way of getting it
to do so.

I would appreciate any further insights into my problem anyone can
give me,

Steve

Rob Dixon

2007-09-21, 7:00 pm

smdspamcatcher@hotmail.com wrote:
>
> On Sep 20, 2:54 am, rob.di...@350.com (Rob Dixon) wrote:
>
>
> Rob,
>
> Actually, $ENV1 and $ENV2 are environment variable that are set by
> someone else's script. They are embedded in the strings that I have
> to process. I'd like to avoid using the %ENV hash or adding yet more
> pattern matching to my program if there is a simpler way of doing
> things.
>
> It's not my exact code, merely the gist of what I am trying to do. As
> I said, the directory I am searching for exists. If I print out the
> $dir_to_check string, it gives me $ENV1/some_dir/$ENV2/another_dir
> and echo $ENV1 and echo $ENV2 typed from the shell expand to give me
> what I expect. Being somewhat new to Perl, I am not entirely sure of
> the behavior of all the commands. Perl seems to be remarkably
> intuitive, and I was hoping that either a file existence check would
> automatically interpret a string containing $VAR_NAME as an
> environment variable, or that there was some simple way of getting it
> to do so.
>
> I would appreciate any further insights into my problem anyone can
> give me,


You know what? This has really annoyed me. You're new to Perl but you
don't like what I've suggested and you want people to keep posting
answers until you see something you like.

What I wrote is good advice: take it. And if you're not familiar with
Perl then don't expect to be able to reliably extract the 'gist' of
your code: post what you actually have.

Rob
smdspamcatcher@hotmail.com

2007-09-22, 7:00 pm

On Sep 20, 9:29 am, smdspamcatc...@hotmail.com wrote:
> On Sep 20, 2:54 am, rob.di...@350.com (Rob Dixon) wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Rob,
>
> Actually, $ENV1 and $ENV2 are environment variable that are set by
> someone else's script. They are embedded
> in the strings that I have to process. I'd like to avoid using the
> %ENV hash or adding yet more pattern matching to
> my program if there is a simpler way of doing things.
>
> It's not my exact code, merely the gist of what I am trying to do. As
> I said, the directory I am searching for exists.
> If I print out the $dir_to_check string, it gives me
> $ENV1/some_dir/$ENV2/another_dir
> and echo $ENV1 and echo $ENV2 typed from the shell expand to give me
> what I expect. Being somewhat new
> to Perl, I am not entirely sure of the behavior of all the commands.
> Perl seems to be remarkably intuitive, and I was
> hoping that either a file existence check would automatically
> interpret a string containing $VAR_NAME as an
> environment variable, or that there was some simple way of getting it
> to do so.
>
> I would appreciate any further insights into my problem anyone can
> give me,
>
> Steve





Never Mind.

I found out how to solve the problem with backquotes:

$dir_to_check = `echo $dir_to_check`;

if (-e $dir_to_check) now finds the appropriate directory.

Steve






Tom Phoenix

2007-09-22, 7:00 pm

On 9/21/07, smdspamcatcher@hotmail.com <smdspamcatcher@hotmail.com> wrote:

> $dir_to_check = `echo $dir_to_check`;
>
> if (-e $dir_to_check) now finds the appropriate directory.


Does it really? When $dir_to_check ends with a newline? Surely you jest.

But just for fun....

my $dir_to_check = 'fred; rm /your/favorite/file';

The shell's echo is not your friend. Whatever it's doing for you, Perl
itself can do better, cheaper, faster, safer.

Cheers!

--Tom Phoenix
Stonehenge Perl Training
Kens

2007-09-23, 6:59 pm

On Sep 21, 3:48 pm, smdspamcatc...@hotmail.com wrote:
> On Sep 20, 9:29 am, smdspamcatc...@hotmail.com wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

Why?
[color=darkred]

Why the mention of pattern matching?
[color=darkred]

If you really want help, post actual code (short but complete proram
that demonstrates the problem).
[color=darkred]

You are quite sure this is what it gives you. I would like to see that
code. By saying this you have probably made a lot of people
disinclined to help you. You not only refuse to post code, but then
you give inaccurate output.
[color=darkred]

$ENV{ENV1} is not simple enough?

or

use Env 'ENV1';

Then $ENV1 would refer to the environment variable.
[color=darkred]
>
>
>
> Never Mind.
>
> I found out how to solve the problem with backquotes:
>
> $dir_to_check = `echo $dir_to_check`;
>


And this is simpler than the other methods?

> if (-e $dir_to_check) now finds the appropriate directory.
>
> Steve


You really have not given enough detail (code, etc).
How is your script being called (evidently not by the script that sets
the environment variable)?
There could be other ways of accomplishing your goal (e.g. passing the
value as an argument to the Perl program), but if you do not give a
good description of your problem, others can only guess at what might
be the best way to handle your problem.

Ken




Sponsored Links







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

Copyright 2008 codecomments.com