in reply to YES you can! Re: setenv in perl
in thread setenv in perl

Clever. Here's what happens.

When you run Perl with exec, it replaces the shell in the same process. This is the key; just running Perl as it is usually done will not have this effect, which is why you got so many "It can't be done" responses. Because environment variables live in a process, the Perl script sees the same environment the shell did.

When you then exec the shell after modifying the environment, the new copy of the shell, still running in the same process, sees those modifications.

I do have a couple observations to add.

This approach only works if you have control over how your Perl script is run.

Unless the Perl script is in the PATH, I found I needed to explicitly specify the directory: ./env.pl

If you do it this way, you do not need to use Inline::C. Simply modifying %ENV will work:

#!/usr/bin/env perl use 5.008; use strict; use warnings; $ENV{test} = 'TURE'; exec $ENV{SHELL} or die "Failed to exec shell '$ENV{SHELL}'\n";

works just as well:

$ echo "PID=$$; test='$test'" PID=87533; test='' $ exec ./exec.pl $ echo "PID=$$; test='$test'" PID=87533; test='TURE'