seank has asked for the wisdom of the Perl Monks concerning the following question:
The problem with the above subroutine example is that I want to hold on to the string that gets printed to STDOUT (or STDERR), but I cannot modify Library.pm. So instead I need to write LibraryWrapper.pm that massages the subroutines of Library.pm to have returnable scalars, arrays, or hashes, etc. . Below demonstrates what I am trying to do:package Library.pm printInformation() { print(STDOUT, "Here is some info, damn its not returning a scalar"); }
Please also bear in mind that I would like a solution that is portable for any platform, and is completely self-contained, ie. all done in one process, (no forks). Lastly, just in case, I know about the backticks operator but it only seems to work on system commands, or more generally commands from the parent environment. It does not work when one perl subroutine calls another in the same process. I'd appreciate the help a lot, thanks.package Library.pm use Library.pm printInformationWrapper() { my @captureSTDOUT; my @captureSTDERR; # Set state so that: # @captureSTDOUT is populated with all STDOUT. # @captureSTDERR is populated with all STDERR. printInformation(); # Set state back to previous if (@captureSTDOUT > 0) { # Deal with it. } return @captureSTDOUT; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: n a perl sub, how do I capture STDOUT, STDERR that comes from another perl sub?
by akho (Hermit) on Aug 17, 2007 at 11:59 UTC | |
by blazar (Canon) on Aug 18, 2007 at 09:57 UTC | |
|
Re: n a perl sub, how do I capture STDOUT, STDERR that comes from another perl sub?
by moritz (Cardinal) on Aug 17, 2007 at 12:02 UTC | |
by seank (Acolyte) on Aug 17, 2007 at 14:05 UTC |