in reply to Could I redirect stdout back into my script?

If you have 5.004 or greater, you can have any section of code "print" to a scalar using the capabilities of IO::Scalar. For example:
require 5.004; use strict; use IO::Scalar; my $output_string = ''; tie *STDOUT, 'IO::Scalar', \$output_string; &stdout_sub(); untie *STDOUT; sub stdout_sub { print "This will not print to screen.\n"; }
This will preserve whatever stdout_sub() printed in the string $output_string.

stephen

Replies are listed 'Best First'.
Re: Re: Could I redirect stdout back into my script?
by runrig (Abbot) on Apr 22, 2001 at 04:01 UTC
    If you want the old STDOUT back, You might want to wrap the code in a block and localize STDOUT:
    { my $output_string = ''; local *STDOUT; tie *STDOUT, 'IO::Scalar', \$output_string; stdout_sub(); }
    Update: My mistake. The untie does seem to take care of restoring the old STDOUT.
      Not a bad idea, but as far as I can tell, it's not necessary. The 'untie *STDOUT' restores the original STDOUT.

      stephen