Perl file handle question...

Joel Meulenberg joelmeulenberg at yahoo.com
Wed Jan 26 15:00:12 CST 2000


Hehe.
Sorry Ed.  That was a severe case of brain flatulence.  Of course
reading from STDOUT will not work.  When I first though about how to do
this, the ideas that came to mind were ugly enough that I wished that
is was possible to somehow read from STDOUT.  Then I though I'd write a
quick script to try it just for the heck of it.  The funny part is that
in my hurry I actually thought that it worked!!!  Of course it didn't;
and when thinking about it in the shower this morning I realized that
it couldn't possibly have worked.

I want to thank everyone on this list for not immediately replying: 
"Joel, you're on drugs or something man!."  : )

Anyway, below is a real solution to your problem (quick hack) and it's
as ugly as I originally thought it had to be.  It forks a kid that
kinda acts like the Unix "tee" pipe fitting tool, but it sends the
emmissions back to the producer (the parent process) via a pipe. 
(Sounds like we're talking about an auto exhaust system doesn't it? : )
 )

#!/usr/local/bin/perl -w
use strict;
# Remember stdout and stdin for later
open(SAVED_STDOUT, ">&STDOUT");
open(SAVED_STDIN, "<&STDIN");
 
# We're gonna have our stdout go to our kid's stdin
pipe(STDIN, STDOUT) or die $!;
 
# And our kid is gonna feed our own stdout back to us on READHANDLE
pipe(READHANDLE, WRITEHANDLE) or die $!;
 
unless (fork()) {
    # Here's the kid code.  It's basically doing a "tee" pipe fitting
    # but the tee feeds back into the parent!
 
    close(STDOUT); close(SAVED_STDIN); close(READHANDLE);
    # The kid needs to make sure the parent's stdout goes to stdout as
normal
    open(STDOUT, ">&SAVED_STDOUT") or die $!;
    close(SAVED_STDOUT);
    select(WRITEHANDLE); $|++; select(STDOUT); $|++;
 
    # Here we just read from the parent and write to stdout, but also
write
    # back to our parent via WRITEHANDLE.
    while (<STDIN>) {
        print $_;
        print WRITEHANDLE $_;
    }
    exit;
}
 
close(STDIN); close(WRITEHANDLE);
open(STDIN, "<&SAVED_STDIN") or die $!;
close(SAVED_STDIN);
$|++;
 
###############################################################################
# Here's your normal program using stdout as it always would, but also
# reading it's own stdout via READHANDLE.
###############################################################################
my @emissions;
for (my $i = 0; $i < 10; $i++) {
   print "$i mississippi\n";
   push @emissions, scalar(<READHANDLE>);
}
 
 
# We're doing this only to see our own stdout from here on.
close(STDOUT);
open(STDOUT, ">&SAVED_STDOUT") or die $!;
 
print "\nHere are my own emissions:\n at emissions\n";



--- Joel Meulenberg <joelmeulenberg at yahoo.com> wrote:
> > Any ideas about this? I want to grab STDOUT from a Perl script
> within
> > the 
> > same script and do some processing on it.
> 
> You should be able to simply read from STDOUT.  e.g.- "<STDOUT>".
> 
> If you'd prefer to read from a filehandle of a different name, you
> can
> dup STDOUT with something like this:
> 
> open(HANDLE, '>-') or die $!;
> 
> +Joel
> 
> __________________________________________________
> Do You Yahoo!?
> Talk to your friends online with Yahoo! Messenger.
> http://im.yahoo.com
> 
__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com



More information about the grand-rapids-pm-announce mailing list