in reply to redirecting STDOUT (of a system call) to a var
Update: I guess I should point out that the child could be mixing perl STDOUT writes with the system calls' STDOUT writes. I think the original poster wanted to run a code ref, including system calls and have all the writes to STDOUT output to a perl variable.#!/usr/bin/perl -w use strict; pipe READER,WRITER or die; my @output; if (my $pid = fork) { # parent, close the WRITER and read from READER close WRITER; while(<READER>) { chomp; push(@output,$_); print "adding $_ to \@output\n"; } # parent now has the system() output in @output, # as well as the line from the child's print() } else { # child, dup WRITER to STDOUT and close WRITER, then # exec() your system call open(STDOUT,">&WRITER"); close WRITER; print STDOUT "This is the child running ls\n"; exec("/bin/ls"); }
|
|---|