Detecting if input is provided
I need to detect if input has been to my script or not.
For example, I would like to differentiate between an invocation from
the two lists below:
Examples where input is "provided":
perl myscript.pl input.txt
perl myscript.pl < input.txt
executable_that_writes_to_sdtout | perl myscript.pl
Example of where no input is "provided":
perl myscript.pl
---
I've tried:
if (seek(STDIN,0,1)) {
#input provided
}
else {
# no input provided
}
which appears to work if an input file was specified or redirection
was done but does not work for piped data.
If (eof(STDIN)) {
}
which blocks if no input was provided.
I'm using Windows XP.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Detecting if input is provided
On 3/14/11 Mon Mar 14, 2011 1:49 PM, "Mark" <google [at] markginsburg.com>
scribbled:
> I need to detect if input has been to my script or not.
>
> For example, I would like to differentiate between an invocation from
> the two lists below:
>
> Examples where input is "provided":
> perl myscript.pl input.txt
> perl myscript.pl < input.txt
> executable_that_writes_to_sdtout | perl myscript.pl
>
> Example of where no input is "provided":
> perl myscript.pl
>
> I'm using Windows XP.
>
The standard Unix function for determining this is isatty(in fd), where fd
is the file descriptor for the input/output stream. I don't know the
equivalent in Windows. However, there is the file test operator -t that
returns true if "Filehandle is opened to a tty", and the POSIX module
includes the isatty function. Try those.
perldoc -f -X
perldoc POSIX
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Detecting if input is provided
On 11-03-14 04:49 PM, Mark wrote:
> Examples where input is "provided":
> perl myscript.pl input.txt
> perl myscript.pl< input.txt
> executable_that_writes_to_sdtout | perl myscript.pl
>
> Example of where no input is "provided":
> perl myscript.pl
>
my $has_input = 0;
while( <> ){
$has_input = 1;
# process the input line by line
}
if( ! $has_input ){
# do something else
}
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Detecting if input is provided
On Mar 14, 2:06=A0pm, jimsgib... [at] gmail.com (Jim Gibson) wrote:
> The standard Unix function for determining this is isatty(in fd), where f=
d
> is the file descriptor for the input/output stream. I don't know the
> equivalent in Windows. However, there is the file test operator -t that
> returns true if "Filehandle is opened to a tty", and the POSIX module
> includes the isatty function. Try those.
Yes! The -t test did the trick.
Thanks Jim.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/