Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

can you seek() on STDIN

by nutshell (Beadle)
on May 04, 2003 at 02:36 UTC ( [id://255413]=perlquestion: print w/replies, xml ) Need Help??

nutshell has asked for the wisdom of the Perl Monks concerning the following question:

After reading STDIN is there a way to reset it and read it a second time?


edited: Sun May 4 16:03:58 2003 by jeffa - title change (was: "STDIN")

Replies are listed 'Best First'.
•Re: can you seek() on STDIN
by merlyn (Sage) on May 04, 2003 at 02:51 UTC
    You can seek any seekable filehandle, including STDIN, but only if it's seekable. {grin}

    It depends on what it's opened on. If it's a terminal or a pipe or a socket, no. If it's a file, yes (usually). The easiest way is to try, and note the success/failure result from seek, and then interpret $! on failure.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: can you seek() on STDIN
by Enlil (Parson) on May 04, 2003 at 02:46 UTC
Re: can you seek() on STDIN
by pzbagel (Chaplain) on May 04, 2003 at 04:00 UTC

    Of course there is!

    @stdin=<>; for $i (0..$#stdin){ print $stdin[$i]; } for $i (0..$#stdin){ print uc $stdin[$i]; }

    With my tongue planted firmly in my cheek I say, "Cheers"

Re: can you seek() on STDIN
by tilly (Archbishop) on May 05, 2003 at 06:04 UTC
    Yes, there is a way to do this.

    See my (bad) hack for it at Re (tilly) 1: How to preserve the value of STDIN.

    Incidentally this is my 3000'th post. Perhaps I should do something special, but the above post is pretty characteristic for me, so I decided to let it stand.

    Besides, with my AM posts, I passed 3000 ages ago, so I consider the old post odometer pretty much broken anyways...

      Actually, it isn't even your 3000'th non-anonymous node.

      tilly's write-up count should be corrected from 3000 to 3016 (3008+8)
      so go to user settings and click submit/stumbit if you want to wipe out the latest affect of the node cache race conditions (and perhaps some bugs that were fixed long ago in your case).

      Just one more reason to not bother trying to post (annoying) "milestone" nodes. (:

                      - tye
Re: can you seek() on STDIN
by nutshell (Beadle) on May 04, 2003 at 04:34 UTC
    Hmm.. I probably should have been more clear as to what exactly my problem is.

    Simplified is it this: In a CGI script I'm working on I need to read STDIN twice, but unfortunately after reading it once STDIN no longer contains anything; its length is zero. So I'm asking if there is a way to get STDIN back after the first read -- I've tried everything, without success.


      No, you can't read that back. That STDIN comes from a pipe, and pipes aren't seekable. If you can't modify the original code for some reason (which seems strange, because you can modify something or you wouldn't ask this), you can always put it into a wrapper.


      Is there ever really a reason to need to read() from STDIN in a CGI script? Perhaps you want But why twice? Reading STDIN in CGI once by yourself is a foolish endeavor, let alone twice.

      If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

        I was trying to work around some rather sticky code. (I can't change the code that does the first read.)

        I guess it's time to give up though.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://255413]
Approved by vek
Front-paged by halley
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2024-07-21 02:14 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.