Re: Read first few lines from command output



On 27 Jul 2005 19:28:45 -0700
"Paul Lalli" <mritty@xxxxxxxxx> wrote:

> 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
>

Yes, that's right, I've mistaken that for the actual problem.
.



Relevant Pages

  • Re: Read first few lines from command output
    ... > Inserting a 'last' does the trick. ... > operator in the head of your while loop after the 'last'. ... Paul Lalli ...
    (comp.lang.perl.misc)
  • Re: Read first few lines from command output
    ... >> Inserting a 'last' does the trick. ... >> close (LOTSOFLINES); ... >> Now perl won't try to get the next line since it never sees the ...
    (comp.lang.perl.misc)
  • Re: Need help understanding how a file input block works
    ... here is a section of the Perl code that I am having ... ^ says 'match if we are at the start of the string', ... 'next' is documented in perldoc perl, in the sextion "Loop Control". ... which will stop you from using globals by accident. ...
    (comp.lang.perl.misc)
  • Re: perl newbie: leaner code ideas
    ... > To learn Perl, I have written a bit of code that needs to do the following: ... Don't declare variables prematurely. ... if the foreach loop contriol variable has already been ... variable as a foreach loop contriol variable without an explicit "my". ...
    (comp.lang.perl)
  • 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)