hapee has asked for the wisdom of the Perl Monks concerning the following question:

I have written a number of large number of perl scripts that we are trying to port to cygwin. Assume an environment variable ENVIRON1 contains the information "I am var1". The perl script uses something like this command:
system << EOD; echo ${ENVIRON1} EOD

On Unix this prints:
I am var1

But on Cygwin this prints:
$ENVIRON1


--> the environment variable does not get resolved.

Within the perl section of the script I can access environment variables without a problem using the %ENV hash i.e.  print $ENV{'ENVIRON1'}; but within the system bracket it seem to not resolve the variable. I really appreciate your help. Thank you.

Replies are listed 'Best First'.
Re: Failure to access Environment variables using Perl system command on cygwin
by ikegami (Patriarch) on Feb 16, 2009 at 22:06 UTC

    Sounds like you're not using a cygwin build of Perl as you say you are, or the cygwin build of Perl uses cmd as the shell like other Windows builds of Perl.

    The proper syntax to interpolate env vars in cmd is %VAR%

    Is there a reason why you prefer to involve a shell?

Re: Failure to access Environment variables using Perl system command on cygwin
by codeacrobat (Chaplain) on Feb 16, 2009 at 22:29 UTC
    try this
    perl -MEnv -e 'print $ENVIRON1'

    print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});
      This actually does print the value of $ENVIRON1, I figured it would as you use the perl syntax.


      Now I tried also:
      Perl.exe -MEnv -e 'system("echo $PATH");
      which also works.

      So the difference seems to be that when I use:
      system("echo $PATH");
      it works fine

      while when I use:
      system << 'EOD'; echo $PATH EOD



      it does not.

      is there a way to make the second version work also?



      The reason I try to set the program up this way is that this code has a number of scripts that communicate with each other through environment variables. I.e. different paths are resolved based on these commands. It is not the cleanest way of doing it, but it has served our company for over 5 years fine. Now we are doing a port to Cygwin and it seem to be significantly more challenging. Thanks for your help.