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

I have a script that runs a set of commands and it needs some env variable to be set before running one of the commands. The problem is that system command launches as a separate process, and any env variables that i set there, are not visible for the context that the script runs in. How can I overcome this?

system("set WALLET=NO"); do some perl commands; #this command needs env variable WALLET=NO to b +e set

Replies are listed 'Best First'.
Re: Run system command in same context of the script itself
by syphilis (Archbishop) on Apr 10, 2023 at 11:59 UTC
    do some perl commands; #this command needs env variable WALLET=NO to be set

    I would think that, before your script runs those perl commands, it can just specify:
    $ENV{'WALLET'} = 'NO';
    Cheers,
    Rob
      it doesn't work. It is a separate context\shell
        > it doesn't work.

        use v5.12.0; use warnings; $ENV{BLA} = "YOU ARE MISTAKEN"; system('echo %BLA%'); # $BLA for linux,et al

        > perl.exe -w d:/Perl/pm/t_system.pl YOU ARE MISTAKEN

        Cheers Rolf
        (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
        Wikisyntax for the Monastery

Re: Run system command in same context of the script itself
by Corion (Patriarch) on Apr 10, 2023 at 11:53 UTC
        The requirement is to set the variable and ran a command that utilizes that variable. What i'm doing is setting the vriable which allows connecting to oracle DB, and then connecting to the DB to make some query.
Re: Run system command in same context of the script itself (Crossposted)
by LanX (Saint) on Apr 11, 2023 at 00:58 UTC
Re: Run system command in same context of the script itself
by ikegami (Patriarch) on Apr 10, 2023 at 13:12 UTC

    [When I read "I have a script that runs a set of commands", I thought you meant a shell script that executed the Perl program showed. A more careful re-reading show this is probably not what you meant. This answer probably won't help you.]


    There's no generic way of making another process do something.

    If the Perl program needs to cause Perl's parent process to do something, you will need devise a way to communicate with it if there isn't already one, and ask it to perform the action using that method of communication. How this should be done will depend on the parent process and what exactly you want to do.

    For example, if the parent is expected to be sh, you could have the program output sh commands to stdout and have the shell capture and execute them.

    x=123 printf '%s\n' "$x" # 123 eval "$( perl -Mv5.14 -e'say "x=456"' )" printf '%s\n' "$x" # 456
      All the commands run in the parent process. But, I need to set some environment variable in order one of the commands to work. To do that, i use system() call, but it spawns a child process, so the variable it sets, is not vsisble for the parent (which carries all the work). Setting the variable is all the child process does. What i'm trying to do is to set the variable that will allow connection to oracle db. then I wnat to connect to the db and query it. The connection and querying are done in the parent process. I don't quite understand what youre saying there.
        I'm puzzled

        If that's not enough, maybe just better show us an SSCCE of what you want, instead of leaving us the burden of interpretation?

        update

        > All the commands run in the parent process.

        Does that mean Perl is just the sub-process and you want to change the ENV of the non-Perl parent?

        I'm afraid that's not possible, w/o adjusting the parent.

        Already on the OS level this is forbidden for security reasons. The parent-process has full control and must actively change its own ENV. It is protected from changes in a sub-process.

        Simplest solution: -> eval-ing a string returned by the Perl sub-process to set ENV

        But now you are also confronted with security implications, because you have to make sure this communication isn't eavesdropped.

        update

        and if all of this doesn't help, please have a look there -> "I know what I mean. Why don't you?"

        Cheers Rolf
        (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
        Wikisyntax for the Monastery

        Setting the variable is all the child process does.

        Yes, you are setting one of the child's variable, but that's not the right variable. You want to set one of the parent's variable. I showed one way of doing this.

        I don't quite understand what youre saying there.

        What's unclear?