in reply to setenv in perl

Prepare to be enlightened:

#!/usr/bin/perl # setenv from within perl to effect parent shell # actually parent should be more appropriately # called "adopted" or "step-parent", re execl # there is one little requirement: # you should run this program via shell's exec: # % exec <scriptname> # # author: bliako # date: 09/07/2018 use Inline C => DATA; # ... # this should be the last statement in your script # because nothing else will be executed after that # not even the 'or die ...' mysetenv("test", "TURE") or die "mysetenv() failed"; __END__ __C__ #include <stdio.h> #include <unistd.h> #include <stdlib.h> int mysetenv(const char *K, const char *V){ if( K==NULL || V==NULL ){ return 0; } char *shell = getenv("SHELL"); if( setenv(K, V, 1) ){ fprintf(stderr, "mysetenv() : call to setenv() has fai +led.\n"); return 1; } // replace caller shell with a new shell which now has // set this new env var //printf("shell to exec is %s\n", shell); execl(shell, NULL, NULL); perror("execl failed, "); return 1; }
% echo $test

% exec env.pl
% echo $test
TURE

The above script sets the specified environment variable and at the same time replaces current program (the perl script) with a new shell which inherits the env. If this script is executed via shell's exec, e.g. % exec env.pl the current shell will be replaced by the new shell created in the perl script and having the set environment.

For more flexibility, the mysetenv() function should be split into 2 parts. The setenv part and the execl part. The execl part should be executed last. After execl call the perl script process stops to exist!

bw, bliako (and excuse my boasting)

Replies are listed 'Best First'.
Re: YES you can! Re: setenv in perl
by bliako (Abbot) on Jul 09, 2018 at 13:56 UTC

    Corion observed that this can be done using only pure Perl. Which is right but somehow escaped me. So here it is in pure Perl. You still need to execute the script via exec env2.pl.

    #!/usr/bin/perl # setenv from within perl to effect parent shell # actually parent should be more appropriately # called "adopted" or "step-parent", re execl # there is one little requirement: # you should run this program via shell's exec: # % exec <scriptname> # # this is pure-Perl implementation after Corion's # comment. # set env $ENV{test} = 'TURE'; # ... # this should be the last statement in your script # because nothing else will be executed after that # not even the 'or die ...' die "no shell!" unless defined($ENV{SHELL}); exec($ENV{SHELL}); __END__
Re: NO you can't ! Re: setenv in perl
by KurtZ (Friar) on Jul 09, 2018 at 16:42 UTC
    Yes exec doesn't spawn a child process but hands over control to another "executable" on the same level, which never returns.

    Your trick only works interactively because you need to exec a NEW follow up shell, waiting for human input.

    This means this illusion won't work in a non interactive script. All code after the exec will be ignored.

      On a more serious node, the exec() is a safe (Edit: not safe as in safety and security) way to achieve what the question asked as my use-case demonstrated. It is true that unless one executes the "trick" from an interactive shell any command past that exec will not run as KurtZ whines rightly points out. For example as a cron job.

      I don't see it as a "trick" because this is what exec() was intended to do in the first place. It is not exploiting any of its features=bugs, it is not using it in a heads-down-feet-up kind of way.

      And definetely the result is not an illusion because it's there. You get your environment modified albeit within a brand new shell which inherits from the parent shell. It inherits env variables and even opened file descriptors. For example:

      exec 3> /tmp/out echo 'before exec' >&3 exec env2.pl echo 'after exec' >&3 exec 3>&- cat /tmp/out before exec after exec

      So, not a trick, not an illusion but limited (and what isn't) to interactive shells.

      For non-interactive use, e.g. a cron-job one can go with corion's Re: setenv in perl. And if only 1 env-variable needs to be set up, then the eval can be avoided by using:

      export test=`perl -e 'print 'TURE'`

      Lastly, if the scenario is to run a shell command which reads data from the environment and having a perl script to calculate this data and export it to the environment then why not let perl calculate AND spawn the command because any system() call inherits perl's environment vars, like so:

      #!/usr/bin/env perl $ENV{test} = 'TURE'; # calculate my @cmd = ('command-exec', 'args'); system(@cmd); # spawn # simple demo: system('echo $test'); # prints what

      bw, bliako

        The same can be achieved with a shell alias or function calling a Perl function and exporting the returned variable value pairs.

        with less code and side effects

      Nope! Not in a Quantum Computer running UNIX. There, in a parallel universe my "trick"'s "illusion" will warp into reality.

        That's kind of Schrödinger's BS, right?

        You need to taste it to find out it's not chocolate! ;P

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: YES you can! Re: setenv in perl
by Anonymous Monk on Jul 09, 2018 at 14:12 UTC

    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'
Re: YES you can! Re: setenv in perl
by Aldebaran (Curate) on Jul 11, 2018 at 16:58 UTC

    I was trying to follow your post as I think triangulating this question with C makes complete sense. Have I missed a step?

    $ touch 1.bliako.pl $ chmod +x 1.bliako.pl $ gedit 1.bliako.pl &

    I paste in your code and set it to execute. First I need Inline.pm: