in reply to Need Help: Capture Print Outputs from a Function
Here's one way
#! perl -sw use strict; sub immutable { print "immutable received args: [ @_ ]\n"; print "Some more stuff\n"; } ## Save a copy of STDOUT open SAVED, '>&=STDOUT'; close STDOUT; ## Open STDOUT to a variable (ramfile)(Requires 5.8.x) open STDOUT, '>', \ my( $var ) or die $!; ## Call the sub immutable( qw[ some args here ] ); ## Close the ramfile close STDOUT; ## Redirect STDOUT back to it's original place open STDOUT, '>&=SAVED' or die $!; ## Discard the backup close SAVED; ## Use the captured output print "The variable \$var now contains:\n'$var'\n"; __END__ C:\test>junk2 The variable $var now contains: 'immutable received args: [ some args here ] Some more stuff '
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Need Help: Capture Print Outputs from a Function
by chromatic (Archbishop) on May 02, 2006 at 20:31 UTC | |
by BrowserUk (Patriarch) on May 02, 2006 at 20:55 UTC | |
by ikegami (Patriarch) on May 02, 2006 at 21:29 UTC | |
by ikegami (Patriarch) on May 02, 2006 at 21:32 UTC | |
by BrowserUk (Patriarch) on May 03, 2006 at 08:30 UTC | |
by ikegami (Patriarch) on May 03, 2006 at 16:34 UTC |