1

Here is the code snippet of main script. The main script is calling populate_details script through pipe. Sending 2 arrays inv1 and inv2 as input.

 *my $fh = FileHandle->new('| ./populate_details') 
 print $fh Data::Dumper->Dump([\@inv1], [qw(inv1)]);
 print $fh Data::Dumper->Dump([\@inv2], [qw(inv2)]);*

The question is , how to capture the return code and return value from populate_details script ? For example, the populate_details script is sending as hash of return values.

Could you Please help ?

4
  • 1
    Tip: No point in using FileHandle. You can use open(my $fh, '| ./populate_details') Commented Oct 3, 2019 at 19:18
  • For sending hashes between processes you could use JSON, see for example JSON::XS Commented Oct 3, 2019 at 20:29
  • Thanks ! Could you please give me an example plz , on how to get the hash from the child script ? Commented Oct 3, 2019 at 22:24
  • Scripts do not return things. Check out perldoc perlmod for how to set up Perl code so it can be called from other Perl code. Commented Oct 4, 2019 at 17:26

1 Answer 1

1

To retrieve the exit status, close the filehandle and check $?

If the filehandle came from a piped open, close returns false if one of the other syscalls involved fails or if its program exits with non-zero status. If the only problem was that the program exited non-zero, $! will be set to 0. Closing a pipe also waits for the process executing on the pipe to exit--in case you wish to look at the output of the pipe afterwards--and implicitly puts the exit status value of that command into $? and ${^CHILD_ERROR_NATIVE}.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks !! If i need to capture the return hash sent by the child script, how to capture it ?
Check out IPC::Open2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.