How to read to and write from a pipe in Perl?

You can use IPC::Open3 to achieve bi-directional communication with child. use strict; use IPC::Open3; my $pid = open3(\*CHLD_IN, \*CHLD_OUT, \*CHLD_ERR, ‘cat’) or die “open3() failed $!”; my $r; for(my $i=1;$i<10;$i++) { print CHLD_IN “$i\n”; $r = <CHLD_OUT>; print “Got $r from child\n”; }

load .profile with proc_open()

I don’t have a ksh, but I’ve managed to do it with bash. /home/galymzhan/.bash_profile: export VAR_FROM_PROFILE=”foobar” /home/galymzhan/test.php: #!/usr/bin/php -n <?php if (!isset($_SERVER[‘VAR_FROM_PROFILE’])) { $descriptors = array(0 => array(‘pipe’, ‘r’), 1 => array(‘pipe’, ‘w’)); $process = proc_open(‘bash’, $descriptors, $pipes); fwrite($pipes[0], escapeshellcmd(‘source /home/galymzhan/.bash_profile’) . “\n”); fwrite($pipes[0], escapeshellcmd(‘/home/galymzhan/test.php’) . “\n”); fclose($pipes[0]); echo “Output:\n”; echo stream_get_contents($pipes[1]); echo “\n”; fclose($pipes[1]); … Read more