Click here to get back home

How to set environmental variables before running another command?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
How to set environmental variables before running another command? Meal 06-03-2008
Get Chitika Premium
Posted by Meal on June 3, 2008, 5:47 pm
Please log in for more thread options
> >> Just set the env vars in the parent process (i.e. your Perl script) and=

> >> the child processes (build script) will inherit them.
>
> >> jue
>
> >Thanks to both of you.
> >It seems there's no other way.
> >The .cmd file to set the variables is rather large, I definitely don't
> >want to set that much variables manually.
>
> Nobody is talking about setting them manually. Just set them in your
> Perl script.
>
> jue

Well, set them "manually" in my perl script.
I want a way that I don't need to take care of the content of the .cmd
file.

Posted by Martijn Lievaart on June 4, 2008, 2:36 am
Please log in for more thread options
On Tue, 03 Jun 2008 14:47:45 -0700, Meal wrote:

> Well, set them "manually" in my perl script. I want a way that I don't
> need to take care of the content of the .cmd file.

Solution #1

Write a perl program to write a commandfile. That commandfile calls the
commandfile with the settings and everything else. The Perl script first
writes the .cmd script with all the commands needed, then execs it.

Solution #2

Or create a wrapper .cmd script:

:: wrapper script
:: First set up the environment
call myenv.cmd
:: Now the environment is set up correctly, call the perl program
perl myprog.pl

( Never use REM, us :: for comments in .cmd scripts )

Solution #3

Or do a dirty trick (untested script)

#!/usr/bin/perl
use strict; use warnings;

use FindBin qw($Bin);

unless ($ENV) {
        $ENV = 1;
        exec "$Bin/myenv.cmd & $0";
        die "Cannot exec";
}

... rest of script ...

In the last two solutions, it is hard to pass on any parameters to the
Perl script (correctly). This is because passing arguments and quoting is
hard in .cmd. Maybe there is a good solution in .cmd language, ask in an
appropriate group for that.

A way around that for the second solution would be to save the arguments
in the environment abd restore later (completely untested):

unless ($ENV) {
        $ENV = 1;
        my $n=0;
        for (@ARGV) {
                $ENV = $_;
                $n++;
        }
        exec "$Bin/myenv.cmd & $0";
        die "Cannot exec";
}

# restore command lines args
for (grep /^MY_ARG_(\d+)/, keys %ENV) {
        $ARGV[$1] = $ENV;
}

... rest of script ...

HTH,
M4

Similar ThreadsPosted
Passing a command to a process that's already running September 19, 2005, 11:09 pm
running shell command without echo in terminal May 21, 2008, 3:50 pm
Trouble running a unix command with a subroutine call November 24, 2005, 7:03 am
problem running grep within system command in perl March 19, 2006, 12:53 pm
Environmental variable for CGI proxy May 16, 2006, 9:50 am
problem in setting environmental variable March 5, 2007, 4:59 am
Static variables for separate command-line calls? January 15, 2006, 11:22 am
How to construct new variables in a script from other variables and strings. May 12, 2006, 4:51 am
local variables and global variables June 27, 2006, 8:31 am
running ppm on XP 64 December 29, 2006, 2:04 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap