Abhinandan
Abhinandan

Reputation: 159

How to export environment variable set in perl script to batch shell?

I am executing a perl script on windows, using a batch script. I am setting below variable in batch script:

SET PATH_VAR=C:\Users\

I am able to access PATH_VAR in perl as below:

my $path1 = $ENV{'PATH_VAR'}

I would like to also export environment variables set in perl to batch. Like the inverse of what I am doing now. Is there a way to do that?

PS: I tried this, but it doesn't work:

$ENV{'PATH_Z'}="Hello World";

Upvotes: 1

Views: 6067

Answers (2)

mob
mob

Reputation: 118645

The only way to do this is to have the Perl script output shell statements, and for the shell to evaluate the output.

Bash example:

$ export FOO=123
$ echo $FOO
123
$ perl -e 'print "export FOO=456\n"' ; echo $FOO
123
$ $(perl -e 'print "export FOO=789\n"') ; echo $FOO
789

Edit: I see OP is using Windows, so this answer doesn't apply :-(

Upvotes: 5

JGNI
JGNI

Reputation: 4013

Changes to environment variables can not effect the parent process, it's part of how they work, so nothing you do in the Perl script can change the environment variables of the batch script. However any child process, started with exec(), system() or `` will see the changes you made in the Perl script.

Upvotes: 6

Related Questions