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!
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; } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: feeding text to a process, and capturing its output, safely
by QM (Parson) on Mar 23, 2004 at 19:49 UTC | |
by merlyn (Sage) on Mar 23, 2004 at 19:52 UTC | |
by QM (Parson) on Mar 23, 2004 at 20:51 UTC | |
by jonadab (Parson) on Mar 23, 2004 at 22:21 UTC | |
Re: feeding text to a process, and capturing its output, safely
by duelafn (Parson) on Jun 09, 2011 at 16:10 UTC | |
Re: feeding text to a process, and capturing its output, safely
by Anonymous Monk on Mar 24, 2004 at 07:50 UTC |
Back to
Cool Uses for Perl