Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8509061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:19:53+00:00 2026-06-11T03:19:53+00:00

I read a few lines from STDIN. How can I pass the remaining of

  • 0

I read a few lines from STDIN. How can I pass the remaining of STDIN to a command that reads from standard input (e.g. md5sum or wc)?

I could do a:

read_a_few_lines_from_diamond_operator();
open (C, "|cmd");
while(<>) { print C }
close C;
cleanup_after_C();

but for efficency reasons I would like not to touch the input, but instead pass the filehandle of STDIN. Sort of like:

seq 10 | (read A; wc)

where read reads as much as it likes before passing the rest on to wc. I cannot use this solution, though, as I need to start the command from inside my perl program and I need to do work after cmd completes.


I read a few lines from the file ‘foo’. How can I pass the remaining to a command that reads from standard input (e.g. md5sum or wc)?

I could do a:

open (F, "<foo");
read_a_few_lines_from_F();
open (C, "|cmd");
while(<F>) { print C }
close C;
cleanup_after_C();

but for efficency reasons I would like not to touch the input, but instead pass the rest of file ‘foo’.


I have a feeling it can to be done using trickery like select, open(FOO,">&STDOUT), exec 6<&0, fork, pipe.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:19:55+00:00Added an answer on June 11, 2026 at 3:19 am

    The answer is quite simple: You don’t need to do anything special. Your child will automatically inherit your STDIN with system and exec. Everything you haven’t read from STDIN will be readable by the child.

    There is a problem, though. Because reading one character at a time would be crazy inefficient, Perl reads from the file a block at a time. That is to say you read more from the file than the “few lines” you got back from Perl. This can clearly be seen using the following command:

    perl -E'say $_ x 500 for "a".."z"' \
       | perl -e'<>; <>; exec("cat");' \
       | less
    

    Instead of starting at the start of the second line, cat starts in the middle of the “q”s (at byte 8192)!

    You’d have to switch from reading lines with readline (<>) to reading individual bytes with sysread if you wanted 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);
    ...
    

    Untested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the code sample from man select plus a few lines to read
I have two (UNIX) programs A and B that read and write from stdin/stdout.
am trying to read few lines from a txt file using JS,and i have
I've read a few answers on here that condemn the use of svn:externals. I
I've read a few topics about programs that combine Windows Forms and console applications,
The next few lines I'm going to write come from the book The C++
I'm trying to read a (small-ish) file in chunks of a few lines at
How can I read a single character/key from the console without having to hit
I'm trying to read a specific line from a file and I can get
I have binary data in a file that I can read into a byte

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.