in reply to Run system command in same context of the script itself

[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

Replies are listed 'Best First'.
Re^2: Run system command in same context of the script itself
by igorle (Initiate) on Apr 10, 2023 at 14:52 UTC
    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?