Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I have combed the archives of this site looking for help with redirecting STDOUT from the Test::ok function temporarily. Unfortunately, all the answers so far work fine in 5.8 but not on 5.6 or require adding a new file to application distribution.

My first attempt was as follows:

# Works with 5.8 only! sub ok_test_wrapper($@) { my ($desc,@params) = @_; my ($stdout); open (OLDOUT, '>&STDOUT') || die "dup() of STDOUT failed: $!"; close STDOUT or die "close STDOUT: $!"; open STDOUT, '>', \$stdout or die "redirect STDOUT: $!"; Test::ok(@params); close STDOUT or die "close STDOUT: $!"; open (STDOUT, '>&OLDOUT') || die "reopen STDOUT: $!"; close OLDOUT or die "close OLDOUT: $!"; chomp($stdout); print "$stdout - $desc\n"; return; } sub ok($;$$) { ok_test_wrapper("$_[[1]]",$_[[0]]); }

Unfortunately, the manpage for open in 5.6 does not allow the use of references in the same way as 5.8. It creates a file by the name 'HASH(0x3859383)' or the like.

p>Using Tie::Handle requires creating another file which on our system requires going through the whole design process again. The preferred solution is a modification to the file this code resides in.

The only option I came up with, which is a hack, is to write/read a temp file...yuck...

# Hack sub ok_test_wrapper($@) { my ($desc,@params) = @_; my ($stdout) = "./foo3.$$"; open (OLDOUT, '>&STDOUT') || die "dup() of STDOUT failed: $!"; close STDOUT or die "close STDOUT: $!"; open STDOUT, '>', $stdout or die "redirect STDOUT: $!"; select STDOUT; $| = 1; Test::ok(@params); close STDOUT or die "close STDOUT: $!"; open (STDOUT, '>&OLDOUT') || die "reopen STDOUT: $!"; close OLDOUT or die "close OLDOUT: $!"; open FH, "<$stdout" or die "reading file failed: $!"; my $tmp = <FH>; close(FH); unlink($stdout); chomp($tmp); print "$tmp - $desc\n"; return; }

Any other ideas?

mgc


In reply to Redirecting STDOUT from internal function with 5.6.1 restrictions by mgc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (12)
    As of 2014-07-31 17:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (249 votes), past polls