[32255] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3522 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Oct 17 09:09:22 2011

Date: Mon, 17 Oct 2011 06:09:03 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 17 Oct 2011     Volume: 11 Number: 3522

Today's topics:
    Re: help with pipe <jwkrahn@example.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sun, 16 Oct 2011 15:09:12 -0700
From: "John W. Krahn" <jwkrahn@example.com>
Subject: Re: help with pipe
Message-Id: <dCImq.927$a23.685@newsfe18.iad>

bjlockie wrote:
>
> my $cmd = 'ping google.ca |';
>
> open( ZAP, $cmd ) || die "can't fork: $!";
>
> print $cmd . "\n";
>
> print "here2\n";
>
> my $numLines = 0;
>
> while (($numLines<  10)&&  (my $line =<ZAP>)) {
>     print $line;
>     $numLines++;
>     }
>
> print "here3\n";
>
>
> That doesn't exit the while loop and it prints the ping output to the
> screen.

Don't do it like that.  You are not testing whether the value returned 
from readline is defined.  A better way would be:


my @cmd = qw/ ping google.ca /;

open my $ZAP, '-|', @cmd or die "Cannot open pipe from '@cmd' because: $!";

print "@cmd\n";

print "here2\n";

while ( my $line = <$ZAP> ) {
     last if $. == 10;
     print $line;
     }

close $ZAP or warn $! ? "Error closing '@cmd' pipe: $!"
                       : "Exit status $? from '@cmd'";

print "here3\n";

__END__



John
-- 
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 3522
***************************************


home help back first fref pref prev next nref lref last post