Re: Read first few lines from command output



Sven-Thorsten Fahrbach wrote:
> On 27 Jul 2005 13:47:26 -0700
> sp0_0ky@xxxxxxxxx wrote:
>
> > open(LOTSOFLINES,"mycommand 2>&1|");
> > count=0;
> > while (<LOTSOFLINES>)
> > {
> > print;
> > count++;
> > if ($count > 10)
> > {
> > close(LOTSOFLINES);
> > }
> > }
> Inserting a 'last' does the trick.
>
> while (<LOTSOFLINES>) {
> print;
> $count++;
> if ($count > 9) {
> close (LOTSOFLINES);
> last;
> }
> }
>
> Now perl won't try to get the next line since it never sees the diamond
> operator in the head of your while loop after the 'last'.

It is not the <> operation that causes the broken pipe error, it is the
close() statement. However, assuming the close *does* work, your last
statement will prevent the "read on closed filehandle" warning for the
remainder of the loop.

Paul Lalli

.



Relevant Pages

  • Re: Read first few lines from command output
    ... >> Inserting a 'last' does the trick. ... >> Now perl won't try to get the next line since it never sees the diamond ... >> operator in the head of your while loop after the 'last'. ...
    (comp.lang.perl.misc)
  • Re: [PATCH 1/1] (v3) SYSVIPC - Fix the ipc structures initialization
    ... a simple testcase is concurrently running an infinite loop on ... So idr_get_newis inserting a pointer into the ... This patch moves the spin_lock_initbefore the call to ipc_addid. ... return err; ...
    (Linux-Kernel)
  • Re: [PATCH 1/1] (v3) SYSVIPC - Fix the ipc structures initialization
    ... a simple testcase is concurrently running an infinite loop on ... So idr_get_newis inserting a pointer into the ... This patch moves the spin_lock_initbefore the call to ipc_addid. ... return err; ...
    (Linux-Kernel)
  • RE: SQL insert record return value?
    ... > I have a loop that includes inserting a record to a datasbase. ... See http://www.QBuilt.com for all your database needs. ...
    (microsoft.public.access.modulesdaovba)
  • Re: Interrupting a loop with a button click
    ... "Graham Mandeno" wrote: ... > event procedure. ... Then the trick is to insert a DoEvents statement in your ... > loop, just before you check whether this boolean has been set. ...
    (microsoft.public.access.formscoding)