1

I thought to do this way. But Top_file not able to receive the data from sub_file.

Top_file.pl

my $top_file_data = "Hi from Top_file\n";

$ENV{'SEND_TO_SUB'} = $top_file_data;
system("perl sub_file.pl");

my $received_data =$ENV{'RECEIVED_DATA'};
say" Message Received from SUB : $received_data";

sub_file.pl

my $data =$ENV{'SEND_TO_SUB'};
say "Message Received from Top : $data";
$ENV{'RECEIVED_DATA'} = "Got Your Message";

Any suggestion how to do it?

Alexx
  • 475
  • 2
  • 8
  • 2
    Perhaps you can share a little bit of your usecase, otherwise it is hard to suggest anything. Take a look at perl [modules](https://perldoc.perl.org/perlmod) , [executing another program and get its returnvalue](https://perldoc.perl.org/perlop#qx/STRING/) or [IPC](https://perldoc.perl.org/perlipc#Bidirectional-Communication-with-Another-Process) – clamp Feb 20 '21 at 10:45
  • 2
    It is not possible to change the environment of a parent process from a sub process, see [Is it possible to change the Environment of a parent process in Python?](https://stackoverflow.com/q/263005/2173773). But instead of using the environment, you could use other forms of communication, for example a JSON file, standard output, signals, pipes, sockets.. – Håkon Hægland Feb 20 '21 at 16:24
  • 1
    To add to other comments: (1) you _can_ make data in a variable available to the child process by using `%ENV` like you do; it may not be the best way though, depending on what kind of data it is (2) The child process can print out what it wants to send to the parent and then the parent captures that. For that the parent should run the child process in a way so that it _can_ capture that output, and that's not `system`. Can do: "pipe open", "backticks (qx)" ... and there are modules, like `Capture::Tiny`, `IPC::System::Simple` and a couple of others. – zdim Feb 20 '21 at 20:08
  • 1
    Docs: [open](https://perldoc.perl.org/functions/open) (search for "pipe"), [qx](https://perldoc.perl.org/perlop#Quote-Like-Operators) (operator form of backticks); [Capture::Tiny](https://metacpan.org/pod/Capture::Tiny), [IPC::System::Simple](https://metacpan.org/pod/IPC::System::Simple) – zdim Feb 20 '21 at 20:14
  • 1
    Some terminology: People use the word "sub" for [subroutine](https://en.wikipedia.org/wiki/Subroutine), so a function --- inside a program (or in a module which gets loaded into the program). What you are asking about is a parent starting another ("child") process and them communicating -- these are different _[processes](https://en.wikipedia.org/wiki/Process_(computing))_ – zdim Feb 20 '21 at 20:18
  • If you still don't know what to do after you've looked over various resources mentioned and/or linked here in comments, or things aren't working out, please let us know . Or ask another, more precise and specific, question – zdim Feb 21 '21 at 22:45

1 Answers1

2

If you want to communicate between two processes, use one of the many tools for that. The perlipc docs show many ways to do this.

Changes to the environment do not propagate upwards, but you can capture output with backticks. Note, you should use the same perl as the original process ($^X) because various environments may already be set and some other perl may be incompatible. Also, I've run into situations where $^X has whitespace, so I quote it:

my $message = `"$^X" sub_file.pl`;
brian d foy
  • 129,424
  • 31
  • 207
  • 592