The answer is quite simple: you do not need to do anything special. Your child will automatically inherit your STDIN using system and exec . Everything that you have not read from STDIN will be readable by the child.
However, there is a problem. Since reading one character at a time would be insanely inefficient, Perl reads a block from a file at a time. That is, you read more from the file than the "few lines" that you returned with Perl. This can be seen with the following command:
perl -E'say $_ x 500 for "a".."z"' \ | perl -e'<>; <>; exec("cat");' \ | less
Instead of starting at the beginning of the second line, cat starts in the middle of "q" s (in byte 8192)!
You will need to switch from reading lines with readline ( <> ) to reading individual bytes with sysread if you want this to work.
Focusing on the bigger picture, I think there is a solution:
open(STDIN, "<", "foo") or die $!; read_a_few_lines(*STDIN); my $pos = tell(STDIN); open(STDIN, "<", "foo") or die $!; sysseek(STDIN, $pos, SEEK_SET); system(@cmd); ...
Or maybe even:
open(STDIN, "<", "foo") or die $!; read_a_few_lines(*STDIN); sysseek(STDIN, tell(STDIN), SEEK_SET); system(@cmd); ...
Unverified.
source share