Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > query status of STDIN

Reply
Thread Tools

query status of STDIN

 
 
Andrei Alexandrescu (See Website For Email)
Guest
Posts: n/a
 
      11-21-2005
Hello,


Here's a tricky one. I have a perl script that most of the time is
invoked like this:

$ some_command | my_perl_script

Now, inside my_perl_script, when reading STDIN through end-of-file, I
want to look at the exit status of the "upstream" command, that is,
some_command. If that command has a nonzero exit status, I deem the
input as incomplete.

How can I do that in perl? Is there a way of figuring out the exist
status of STDIN, in case that came as a pipe?


Thanks,

Andrei
 
Reply With Quote
 
 
 
 
Andrei Alexandrescu (See Website For Email)
Guest
Posts: n/a
 
      11-21-2005
Andrei Alexandrescu (See Website For Email) wrote:
> How can I do that in perl? Is there a way of figuring out the exist
> status of STDIN, in case that came as a pipe?


s/exist/exit/

Andrei
 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      11-21-2005
Andrei Alexandrescu (See Website For Email) wrote:
> Andrei Alexandrescu (See Website For Email) wrote:
>
>> How can I do that in perl? Is there a way of figuring out the exit
>> status of STDIN, in case that came as a pipe?


STDIN does not have an exit value.

You'll need to create the pipe yourself, and check the value
returned from close().

linux% perl -e 'open P,shift()."|" or die; while(<P>){print;} $e=close
P; print "Pipe close = ",0+$e,"\n"' false
Pipe close = 0
linux% perl -e 'open P,shift()."|" or die; while(<P>){print;} $e=close
P; print "Pipe close = ",0+$e,"\n"' true
Pipe close = 1
linux% perl -e 'open P,shift()."|" or die; while(<P>){print;} $e=close
P; print "Pipe close = ",0+$e,"\n"' date
Sun Nov 20 20:54:51 PST 2005
Pipe close = 1

The value of close(P) is true if the process feeding the pipe exited
without an error code.
-Joe
 
Reply With Quote
 
brianr@liffe.com
Guest
Posts: n/a
 
      11-21-2005
"Andrei Alexandrescu (See Website For Email)" <(E-Mail Removed)> writes:

> Hello,
>
>
> Here's a tricky one. I have a perl script that most of the time is
> invoked like this:
>
> $ some_command | my_perl_script
>
> Now, inside my_perl_script, when reading STDIN through end-of-file, I
> want to look at the exit status of the "upstream" command, that is,
> some_command. If that command has a nonzero exit status, I deem the
> input as incomplete.
>
> How can I do that in perl? Is there a way of figuring out the exist
> status of STDIN, in case that came as a pipe?


The exit status of a program is generally only available to its
parent, which in this case would have to be my_perl_script. Your Perl
script could execute some_command itself, and replace its STDIN with
the piped output. For example:

----------------------------------------------------------------------
use strict;
use warnings;

use Getopt::Std;

my %opts;
getopts("c:", \%opts) or die "Bad switch\n";

if (defined $opts{c}) {
open STDIN, "$opts{c} |"
or die "Failed to start command '$opts{c}': $!\n";
}

while (<>) {
print "IN: $_";
}

if (defined $opts{c}) {
close STDIN;
print "Incomplete\n" if $?;
}
----------------------------------------------------------------------

This could then be executed as

$ my_perl_script -c "some_command arg1 arg2"

For more info see:

perldoc perlopentut
perldoc -f open
perldoc perlvar

HTH

--
Brian Raven

It's easy to solve the halting problem with a shotgun.
-- Larry Wall in <(E-Mail Removed)>
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-21-2005
Purl Gurl <(E-Mail Removed)> wrote:
> Joe Smith wrote:
>
>> Andrei Alexandrescu wrote:
>> > Andrei Alexandrescu wrote:

>
> (snipped)
>
>> >> How can I do that in perl? Is there a way of figuring out the exit
>> >> status of STDIN, in case that came as a pipe?

>
>> STDIN does not have an exit value.

>
> Actually, it does.



No it doesn't.

An "exit value" has nothing to do with input/output nor filesystems.

An "exit value" is produced by a "process".

The _program_ that is writing to perl's STDIN has an "exit value",
that is, the program on the left of the pipe.


> This exit value is an "end of file" (eof) signal.



Special characters in an input stream are neither an "exit value"
nor a "signal".


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Mark Clements
Guest
Posts: n/a
 
      11-22-2005
Purl Gurl wrote:
> Tad McClellan wrote:


>> The _program_ that is writing to perl's STDIN has an "exit value",
>> that is, the program on the left of the pipe.

>
> "Left" of a pipe? Your left or my left? What is on the "right"
> of the pipe? If you were so ignorantly arrogant, you might
> become knowledgeable enough to employ correct terms,
> such as input, output, read and write.


You know full-well what he means. I hope you are enjoying your game,
whatever it is.

my $pipe_h;
open $pipe_h, "/bin/ls |" or die $!;
while(my $line = <$pipe_h>){
print $line;
}

Here, /bin/ls is to the left of the pipe. It is clear enough from the
context in the post previous to yours that "pipe" refers to "pipe symbol".

>
>>> This exit value is an "end of file" (eof) signal.

>
>> Special characters in an input stream are neither an "exit value"
>> nor a "signal".

>
> An eof signal is not a special character nor special characters.
> This is a signal a data handling process is finished, with success.
> This signal is and can be generated by many methods, including
> perl core and including your own code.
>


"signal", at least as far as perl is concerned, means something very
specific: it means a traditional unix signal that a process receives
from another process (or itself) that it can generally act upon or
ignore, for example, HUP and INT. It is a basic building-block of
interprocess communication, though according to perldoc perlwin32 "may
not behave as on unix platforms" on Windows. Check out

perldoc perlipc

eof is a value returned from a function that reads from a filehandle to
denote the end of file or an error condition.

There is no mention of signals in

perldoc -f eof

Mark
 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      11-22-2005
Also sprach Purl Gurl:

> Mark Clements wrote:


>> There is no mention of signals in
>> perldoc -f eof

>
> Both Perl Developers and Perl Porters refer to eof as a signal. This
> is documented "all over the place" especially in Perl 6 documentation
> and developer discussions.


Let's test that claim:

ethan@ethan:~/Projects/APC/perl-current$ grep -r -i 'eof signal' *
ethan@ethan:~/Projects/APC/perl-current$

Your "especially in Perl 6 documentation" implies that it should be
mentioned in the perl5 source tree as well, somewhere. But not
surprisingly, it isn't.

> Now what are you to do? Troll and insult Perl Developers and Porters?


And you're just an ordinary pathetic liar.

> Will you tell them how wrong they are?


Given the above empty grep, they are right: There is no such thing as an
eof signal.

> There are thousands of pieces of documentation, thousands of manuals,
> thousands of references, thousands of people, all of which or whom, refer
> to eof as a signal.


There is no such thing as an eof signal.

> Correcting those written materials and experts, should keep you boys
> busy for decades to come.


Those written materials that don't exist as we've seen above.

> Best get busy because there is equal amounts of sources which refer
> to eof as "flag," "marker," "exit code," "exit status" and other terms.


Eof flags and markers are ok, eof exit codes and eof exit status don't
exist.

> Filehandles do have and display exit codes despite your contrary rants.


Filehandles don't exit whereby they cannot have an exit code.

> My game is major league, with an intellectual bullet.


Your game is major league trolling, with an intellect roughly as big as
a cherry pit.

Tassilo
--
use bigint;
$n=71423350343770280161397026330337371139054411854 220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($ m+=<=200);
 
Reply With Quote
 
Dave Weaver
Guest
Posts: n/a
 
      11-22-2005
On Mon, 21 Nov 2005 20:40:15 GMT, Purl Gurl <(E-Mail Removed)> wrote:
> Tad McClellan wrote:
> > Special characters in an input stream are neither an "exit value"
> > nor a "signal".

>
> An eof signal is not a special character nor special characters.
> This is a signal a data handling process is finished, with success.


Really?

So how does a data handling process signal it is finished without
successs? By leaving the file open?


 
Reply With Quote
 
Mark Clements
Guest
Posts: n/a
 
      11-22-2005
Purl Gurl wrote:

> Periodically, I leave here. Sometimes for months, sometimes
> for over a year. Recently I returned. Within two hours of my first
> post, at night, many of you boys were posting both ignorant
> and mean spirited troll articles directed at me. Archives well
> evidence those events. Literally in a matter of minutes, you
> boys were posting ignorantly laughable troll articles upon
> my first few returning posts here.


It is very simple: you continue to post demonstrably incorrect
information and people will continue to correct you. The fact that you
take such gleeful offence at being corrected is just a bonus.

> Recently a bunch of you ignorant trolls started ranting about
> URL decoding and my posted method. A few days later, a
> Perl FAQ appears displaying the same code method I use,
> a participant or two discussed methods same as mine, and
> the Perl FAQ maintainer agreed to add code much like mine.
> They were not trolled.
>


You (predictably) miss the point. Several possible solutions were
posted, one of which was yours. You then posted timing data in an
attempt to prove that because your submission executed faster it was
superior. Issue was taken with your misappraisal (again) of the aims of
programming, not with your solution per se.

<snip rant>

>> my $pipe_h;
>> open $pipe_h, "/bin/ls |" or die $!;
>> while(my $line = <$pipe_h>){
>> print $line;
>> }
>>
>> Here, /bin/ls is to the left of the pipe. It is clear enough from the
>> context in the post previous to yours that "pipe" refers to "pipe symbol".

>
> No. That is a read process, not a "left of the pipe" process. I suggested you
> boys learn to use correct terminology, input, output, read, write and such,
> and here you are defending use of incorrect terminology; street slang.


In effect, ls is writing to the pipe, not reading. How is it a "read
process"?

Mark
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-22-2005
Purl Gurl <(E-Mail Removed)> wrote:
> Dave Weaver wrote:
>> Purl Gurl wrote:
>> > Tad McClellan wrote:

>
>> > > Special characters in an input stream are neither an "exit value"
>> > > nor a "signal".

>
>> > An eof signal is not a special character nor special characters.
>> > This is a signal a data handling process is finished, with success.

>
>> Really?

>
>> So how does a data handling process signal it is finished without
>> successs? By leaving the file open?

>
> A process cannot "finish" without success.



A process most certainly can fail to perform its designated task.

Failure and success are mutually exclusive.


> I have suggested many times to research and read to learn.



Yeah, but nobody listens to you for some reason.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
peek at stdin, flush stdin Johnathan Doe C Programming 5 05-17-2013 04:30 PM
How to pass stdin of a C++ program to the stdin of a process createdwith ShellExecute() Ben C Programming 2 08-29-2009 09:47 PM
STDIN, OUT, ERR and $stdin, out, err - Differences? Terry Cooper Ruby 7 06-09-2009 05:48 AM
Reading from stdin then launching a program that reads from stdin strange behaviour Stefano Sabatini Perl Misc 6 07-29-2007 10:38 PM
Reading stdin once confuses second stdin read Charlie Zender C Programming 6 06-21-2004 01:39 PM



Advertisments