Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Streaming to Handles

by Stevie-O (Friar)
on May 01, 2004 at 18:52 UTC ( #349663=note: print w/ replies, xml ) Need Help??

in reply to Streaming to Handles

If what you're doing is fork-friendly (i.e. doesn't involve modifying global variables), Perl has a built-in syntax just for you -- with open. (This sort of thing, by the way, is why I occasionally reread the perl PODs.)

#foo() is the sub that outputs a lot of data my $pid = open(SUBOUT, '-|'); defined($pid) or die "Fork failed: $!"; if ($pid == 0) { $|=1; foo(); exit(0); } while (<SUBOUT>) ...
The open will fork your script, and the forked copy (the one that calls the function) will have its STDOUT piped to the filehandle given to open() (in this case, SUBOUT).
Here's a quick example I crafted:
#!/usr/bin/perl -l defined($tmp = open(FOO, '-|')) or die "Can't fork: $!"; if ($tmp==0) { print "Roses are red\nViolets are blue\nI think Perl rules\nAn +d you should too\n"; exit(1); } print for map {chomp; "[$tmp] {$_}"} <FOO>;
$"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc

Comment on Re: Streaming to Handles
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://349663]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2015-11-26 01:07 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (695 votes), past polls