STDOUT

Stewart C. Russell scruss-rieW9WUcm8FFJ04o6PK0Fg at public.gmane.org
Sun May 16 15:41:47 UTC 2004


Alan Cohen wrote:
> I have a perl program that gets its input from another program's STDOUT.
> 
> Is there a way that doesn't require the intermediate file?

Yes, many. Before I give a solution, I'd recommend that Perl Monks 
<http://perlmonks.org/> will give better answers than any generic Linux 
  group. Though there are good Perl people on this list (hi tom, hi 
talexb) Perl Monks will have the answer to your question.

Rather than writing to an intermediate file, Kenin suggested slurping 
the whole file into an array. There are many ways to do this. Here's an 
easier to follow, but slightly verbose method:

#!/usr/bin/perl -w
# a toy program that will print all of STDIN to STDOUT if it found
# a line with just the number 7 on its own.

use strict;
my @lines;
while (<>) {

     # you might want to do some pre-processing here
     push @lines, $_;    # append every line to the array
}

print @lines if grep( /^7$/, @lines );
exit;

As ever, TIMTOWTDI.

  Stewart

-- 
$,="\n";foreach(split('',"\3\3\3c>\0>c\177cc\0~c~``\0cc\177cc"))
{$a++;$_=unpack('B8',$_);tr,01,\40#,;$b[$a%6].=$_};print @b,"\n"
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list