Re: What is the source of my input, file or STDIN?

From: Drieux (drieux_at_wetware.com)
Date: 01/07/04


Date: Wed, 7 Jan 2004 10:10:01 -0800
To: Perl Beginners Mailing List <beginners@perl.org>


On Jan 6, 2004, at 1:07 PM, david wrote:
> pgregory@micron.com wrote:
>> Case 3. (this is the difficult case for me) the script is invoked
>> with no file and no pipe to it. I would like the script to
>> end quietly, such as
>>> test.input.source
>>>
>>
>> Instead, it waits for input.
>>
>>> test.input.source
>> no command line args - switching to STDIN
>>
>> and now it waits forever.
>
> select / IO::Select is what you are looking for.
[..]

My compliments to David, as always.

The reason the code is 'waiting for ever' in
the case of having no STDIN is that it is in a
blocking IO Read on STDIN.

So since we have a better spec, I have updated the code
to show how the IO::Select could be used to gate
for the case that the code was called without
command line input, nor a current connection for STDIN.

<http://www.wetware.com/drieux/pbl/perlTrick/CommandLine/
file_or_stdin.plx>

Note all of the noisy 'print statements' are there
merely to show the transition of the logic of the code.

On Jan 6, 2004, at 12:53 PM, Steve Grazzini wrote:

> On Jan 6, 2004, at 3:17 PM, pgregory@micron.com wrote:
>> Case 3. (this is the difficult case for me) the script is
>> invoked with no file and no pipe to it. I would like the
>> script to end quietly
>
> die usage() if @ARGV == 0 and -t;

You might not want to test if there is a
controlling terminal - since that would prevent
the pipe fitting from working unless there was a
controlling terminal. A problem that will crop up
when JoeBob opts wants to use the pipe fitting in
their KSH script...

> I didn't show you how to check for the pipe (-p) because
> this should probably work, too:
>
> % your-script <input.txt

We have Sooooo got to talk about your meds here...

8-)

To be honest, I had not thought about the idea of
'-p' even trying to test to see if it was a pipe
since it is-ish when one connects the pipe fittings
of the standard shell, or as you have done, the
express redirection of input...

[jeeves: 30:] wc -l funk*
       14 funk_env.plx
[jeeves: 31:] ./file* < funk*
no command line args - switching to STDIN
STDIN had 14 number of lines
we saw 14 number of lines
[jeeves: 32:]

The 'redirect in' of "<" simply is a way of changing
how STDIN is feed to the calling program by the shell.
It of course is dependent upon how the shell copes
with re-attaching STDIN|STDOUT combo's...

ciao
drieux

---


Relevant Pages

  • Re: A bit of grep and regex help?
    ... And here I was thinking redirection was implemented via close and open (ie ... the shell process having nothing to do as the stdin of the command ... connect write of pipe to stdout ...
    (alt.os.linux)
  • Re: How to know if read is pending on pipe
    ... > programs may use stdin and stdout to interface with the operator. ... this seems to be a case for using the pipe mechanism. ... > there is no read pending. ... I cannot predict when and if the script wants ...
    (comp.unix.aix)
  • Re: How does one effect O_NONBLOCK?!
    ... once I've created 3 pipes (for stdout, stderr and stdin) ... child and parent processes and then dup2the 3 descriptors into the ... corresponding ends of the pipes in the child before execve-ing. ... end side of the pipe that's connected to stdin of the child process. ...
    (comp.unix.programmer)
  • Re: $stdin
    ... i have write a little send_mail script. ... another program, like cat /var/mailtext| send-mail.rb ... this is runing very well, but when no text is in the pipe, the program ... my question is, how can i ask, if text in $stdin, without the program is ...
    (comp.lang.ruby)
  • Re: man-in-the-middle translation of input
    ... program that spawns the game, forwards its output to stdout, reads my input ... replaces my input by a series of valid game commands. ... write to stdout, I couldn't find a way to read from stdin, preprocess the ... input and feed it through a pipe to the child. ...
    (comp.unix.programmer)