CUFP
merlyn
snippet
<div class="Description">When you have program data that must be fed as the standard input to a process, and you want to capture its output, and you want to avoid the shell, it's time for a double fork!</div>
<CODE>
my @INPUT = (... many lines of text ...);
my @COMMAND = qw(tr a-zA-Z A-Za-z); # swap lower/upper case
my @RESULT; # this is where the result will go...
if (open RESULT, "-|") { # original process
@RESULT = <RESULT>;
} else { # child
if (open STDIN, "-|") { # child
exec @COMMAND;
die "Cannot exec: $!";
} else { # grandchild
print @INPUT;
exit 0;
}
}
</CODE>