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 | [reply] [d/l] |
|
|
it doesn't work. It is a separate context\shell
| [reply] |
|
|
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
| [reply] [d/l] [select] |
|
|
Re: Run system command in same context of the script itself
by Corion (Patriarch) on Apr 10, 2023 at 11:53 UTC
|
my $environment = readpipe("set WALLET=NO && set");
# parse environment from $environment
| [reply] [d/l] |
|
|
If the requirement of the OP really is to get the ENV of a sub process I'd rather prefer something like this
my $bashcode=<<'__bash__';
. /tmp/src.sh;
perl -MData::Dumper -e 'print Dumper \%ENV';
__bash__
my $VAR1;
eval qx{bash -c "$bashcode"};
print $VAR1->{MONK}; #> jfroebe
Taken from Re^3: How to "source" a shell file in Perl? (Trojan Dump)
| [reply] [d/l] |
|
|
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.
| [reply] |
Re: Run system command in same context of the script itself (Crossposted)
by LanX (Saint) on Apr 11, 2023 at 00:58 UTC
|
Crossposted on StackOverflow
It is considered polite to inform about crossposting, so people do not waste their effort in one site not knowing about the progress at the other one.
| [reply] |
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
| [reply] [d/l] [select] |
|
|
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.
| [reply] |
|
|
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?"
| [reply] |
|
|
| [reply] |