redirecting STDOUT 
Author Message
 redirecting STDOUT

Hi all. Is there a way to redirect STDOUT to both a file and to standard
out? I have a large set of CGI scripts (with many many print statements)
and I need to log all output that would to a file AND still send it to
the web browser.

select() would let me send it to a file but then how do I get it to go
to STDOUT also?

Thanks a bunch,

- Ian



Tue, 17 Sep 2002 03:00:00 GMT  
 redirecting STDOUT
I don't know if there is a way to assign 2 output streams to one File
descriptor but I doubt that that is possible.  What I would do is to write
another perl script to go through all of your cgi scripts and change print
to myprint or something like that.  Then write a module MyPrint.pm, in that
module have a function called myprint which prints to STDOUT and also to a
file using different print commands.  Something like this

sub myprint
{

    open MYFILE,">>myfile";

    close MYFILE;

Quote:
}

If you create the module as an object then you could probably keep the file
open, so you don't have the overhead of opening and closing the file, but to
be honest I don't think it will be necessary.

Cheers
Matt

Quote:

> Hi all. Is there a way to redirect STDOUT to both a file and to standard
> out? I have a large set of CGI scripts (with many many print statements)
> and I need to log all output that would to a file AND still send it to
> the web browser.

> select() would let me send it to a file but then how do I get it to go
> to STDOUT also?

> Thanks a bunch,

> - Ian

--
(_heers

|\/|att |)ale



Tue, 17 Sep 2002 03:00:00 GMT  
 redirecting STDOUT
[Another jeapordy-style post reordered to protect the sane.]

Quote:


> > Hi all. Is there a way to redirect STDOUT to both a file and to
> > standard out?

See perlfaq5: How do I print to more than one filehandle at once?

Basically, it's pretty simple if you're on *nix:

open(FH, "| tee file1 file2") or die "Couldn't tee: $!\n";

For you, I think that

open(STDOUT, "| tee filename") or die "Couldn't tee: $!\n";

would do what you want, but I'll admit that I haven't tested it.

Quote:
> I don't know if there is a way to assign 2 output streams to one File
> descriptor but I doubt that that is possible.

Have you tried? Perl doesn't support it directly, but it is possible.

Quote:
> sub myprint
> {

>     open MYFILE,">>myfile";

>     close MYFILE;
> }

For one-shot deals [from the FAQ]:

for $fh (FH1, FH2, FH3) { print $fh "whatever\n"}

-mjc



Tue, 17 Sep 2002 03:00:00 GMT  
 redirecting STDOUT

Quote:

>> I don't know if there is a way to assign 2 output streams to one File
>> descriptor but I doubt that that is possible.

>Have you tried? Perl doesn't support it directly, but it is possible.

It must be possible using a tied filehandle. See PERLTIE, the section on
TIEHANDLE. In short, you tie a handle to a class (package), and every
print() you do to this handle will be processed by a PRINT method, which
you have to provide. Maybe something like this will do for simple cases,
with one tied handle and predefined output filehandles (but untested):

        sub PRINT {
            my $r = shift;  # object, identifies tied handle; ignored


        }          

A more elaborate version can be devised with incorporating several
handles into the object at "tie time", which can be printed to, in a
loop, in sub PRINT.

   HTH,
   Bart.



Wed, 18 Sep 2002 03:00:00 GMT  
 redirecting STDOUT

Quote:

> Hi all. Is there a way to redirect STDOUT to both a file and to standard
> out? I have a large set of CGI scripts (with many many print statements)
> and I need to log all output that would to a file AND still send it to
> the web browser.

> select() would let me send it to a file but then how do I get it to go
> to STDOUT also?

Check out IO::Multiplex:
http://search.cpan.org/doc/BRUCEK/IO-Multiplex-1.00/Multiplex.pm

David



Fri, 20 Sep 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Redirect STDOUT from a Unix script to a perl program and then redirect back to script

2. how to temporarily redirect stdout?

3. Redirecting STDOUT to a File and then Restoring It

4. REDIRECTING STDOUT

5. How to redirect STDOUT?

6. Help: redirected STDOUT lost (buffer overflow?)

7. synchronistaion of STDIN with redirected STDOUT

8. redirect STDOUT of entire program?

9. assigning STDERR whild redirecting STDOUT to a file

10. How get back redirected STDOUT to its default?

11. Problem with redirecting STDOUT and STDERR

12. Redirecting STDOUT to Socket

 

 
Powered by phpBB® Forum Software