Fwd: Re: [Za-pm] no STDIN

Jason Armstrong jason at dart.co.za
Wed Dec 29 17:57:27 CST 2004


[ Spike: Dec 22 14:45 ]

> male -a myfile spike at mweb.com
> 
> The problem is that it just hangs waiting for something on STDIN.

> Anyone have an idea on how I can get the script to realize when there
> will be no STDIN - (other than another switch on the Command line)?

Unfortunately, if you need to read anything from STDIN, then your script
is going to wait until it reads something.

You could just press ^D after the command to get the script to stop
reading.

Or you could reverse the order of command line switches ie provide a
switch that will tell you 'read from stdin':

$ male -a myfile -s spike at mweb.com

The -s could indicate that you wanted to add text to your message.

Then you could do something like:

if ($readstdin) {
  while (<STDIN>) {
    .
    .
  }
}

Or you could have another switch eg '-a myfile' could mean 'attach my
file and read from stdin', whereas '-i myfile' could mean 'attach my
file and send immediately'.

Another way would be to implement a timeout on stdin, using select().
For example:

my $in = '';
vec($in, fileno(STDIN), 1) = 1;
if (select($in, undef, undef, 2)) {
  while (<STDIN>) {
    ....
  }
}

Would wait for 2 seconds for input on stdin, and then timeout. Look at
the documentation for select (perldoc -f select) for more information.

--
Jason Armstrong



More information about the Za-pm mailing list