Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Redirecting STDOUT from internal function with 5.6.1 restrictions

by Jenda (Abbot)
on Oct 12, 2004 at 18:22 UTC ( #398641=note: print w/ replies, xml ) Need Help??


in reply to Redirecting STDOUT from internal function with 5.6.1 restrictions

Looks to me like the docs of Test.pm need to be improved. If you look into the source you can see that the module makes a reference to the STDOUT as soon as it's used and then prints to that reference.

Anyway it seems that all you need to do is this:

open $Test::TESTOUT, '>', $stdout;
to redirect the output and
close $Test::TESTOUT; $Test::TESTOUT=*STDOUT{IO};
to switch it back.

Jenda
We'd like to help you learn to help yourself
Look around you, all you see are sympathetic eyes
Stroll around the grounds until you feel at home
   -- P. Simon in Mrs. Robinson


Comment on Re: Redirecting STDOUT from internal function with 5.6.1 restrictions
Select or Download Code
Re^2: Redirecting STDOUT from internal function with 5.6.1 restrictions
by mgc (Novice) on Oct 12, 2004 at 19:10 UTC
    For a minute there I thought you were on to something, but
    alas it turned out to be an incorrect assumption. Here is
    how I see it...
    
    In the code Test module Test.pm 'makes a copy' of the STDOUT filehandle
    
    
    $TESTOUT = *STDOUT{IO};
    This has the same effect as below:
    open( TESTOUT, ">&STDOUT" ) || die "dup failed";
    In either case Perl comes back complaining that: "A file or directory in the path name does not exist at ...". Which means that the last parameter in the 5.6.1 version of open must point to a string with a path/filename. mgc

      OK then. I should have tested my suggestions. Anyway this does seem to work fine in Perl 5.6.1:

      use FileHandle; use Test; plan(tests => 3); ok("first"); { local $Test::TESTOUT; open $Test::TESTOUT, '>', "$ENV{TEMP}/zkTestRedirect.txt"; ok("second"); } ok("third");

      Jenda
      We'd like to help you learn to help yourself
      Look around you, all you see are sympathetic eyes
      Stroll around the grounds until you feel at home
         -- P. Simon in Mrs. Robinson

        This node was taken out by the NodeReaper on Wed Oct 13 06:57:48 2004 (EST)
        Reason: poqui duplicate

        For more information on this node visit: this

        Thanks for the input. It seems thought, your example is still writing to a file. I am trying to trap STDOUT and redirect to a variable(memory).

        mgc

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://398641]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2014-08-20 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (114 votes), past polls