Practical mod_perl / HTML Book /



previous page: 6.4.4. STDIN, STDOUT, and STDERR Streams
  
page up: HTML Version of the book
  
next page: 6.4.6. print( )

6.4.5. Redirecting STDOUT into a Scalar Variable


Sometimes you encounter a black-box function that prints its output to the default file handle (usually STDOUT) when you would rather put the output into a scalar. This is very relevant under mod_perl, where STDOUT is tied to the Apache request object. In this situation, the IO::String package is especially useful. You can re-tie( ) STDOUT (or any other file handle) to a string by doing a simple select( ) on the IO::String object. Call select( ) again at the end on the original file handle to re-tie( ) STDOUT back to its original stream:

my $str;
my $str_fh = IO::String->new($str);

my $old_fh = select($str_fh);
black_box_print( );
select($old_fh) if defined $old_fh;

In this example, a new IO::String object is created. The object is then selected, the black_box_print( ) function is called, and its output goes into the string object. Finally, we restore the original file handle, by re-select( ) ing the originally selected file handle. The $str variable contains all the output produced by the black_box_print( ) function.

 

Continue to:

  • prev: 6.4.4. STDIN, STDOUT, and STDERR Streams
  • Table of Contents
  • next: 6.4.6. print( )







TOP
previous page: 6.4.4. STDIN, STDOUT, and STDERR Streams
  
page up: HTML Version of the book
  
next page: 6.4.6. print( )


Menu

  • HTML Book
  • PDF Book
  • Download Code
  • Table of Contents
  • Errata
  • mod_perl2 User's Guide
  • Sitemap

Search


Add to Google




Creative Commons License


Written by
Eric Cholet (Logilune) and
Stas Bekman (StasoSphere & Free Books).


[ Privacy Policy | Terms of Use | About Us | Search ]

© 2007 StasoSphere.com