Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Redirecting STDOUT

by hesco (Deacon)
on Feb 11, 2007 at 12:38 UTC ( [id://599451]=perlquestion: print w/replies, xml ) Need Help??

hesco has asked for the wisdom of the Perl Monks concerning the following question:

Poking about in the code for Test::Harness and several responses in SuperSearch is not getting me to the promised land. So permit me please to ask here.

I need to redirect STDOUT, and probably STDERR, before its all over, for a test suite, analyze the results and take appropriate action based on that analysis. I'm finding it quite a challenge to figure out the right incantation which will accomplish this. I've left one of my tests commented out.

The print statement is writing to the file. But the Test::More->diag() calls are not making it. Neither are the results of the Test::More tests run in the subroutine call to the $self->_test_site() method.

I'd certainly appreciate any ideas. -- Hugh

{ local *STDOUT; my $log_file = '/tmp/test_sites_output'; # open(STDOUT,"| tee $log_file"); open(STDOUT,'>',"$log_file"); print "Testing the redirected STDOUT filehandle.\n"; foreach my $site (keys %{$sites}){ diag("Next . . . $site"); push @sites, $site; @url = @{$sites->{'url'}}; @expected = @{$sites->{'expected'}}; diag("The url is $url[0]"); diag("We expect the find: $expected[0]"); $self->_test_site($agent,$url[0],$expected[0]); # $self->_test_links($agent,$url); # $self->_test_valid_html($url); } close STDOUT; }
if( $lal && $lol ) { $life++; }

Replies are listed 'Best First'.
Re: Redirecting STDOUT
by Sidhekin (Priest) on Feb 11, 2007 at 13:14 UTC

    Test::More->diag() and other test methods are not printing to STDOUT, but to three Test::Builder file handles that default to (dupes of) STDOUT/STDERR/STDOUT respectively.

    If you want them all to print to your local STDOUT, I guess this would do it:

    my $Test = Test::Builder->new; my @handle_names = qw/ output failure_output todo_output /; my %old; $old{$_} = $Test->$_ for @handle_names; $Test->$_(\*STDOUT) for @handle_names; eval { run_your_tests }; # and restore: $Test->$_($old{$_}) for @handle_names;

    For more detail, see Test::Builder.

    Update: I couldn't stand to see the cut-and-pasted list of handle names, so I added a variable for it. How's that for obsessive? :-P

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

Re: Redirecting STDOUT
by TOD (Friar) on Feb 11, 2007 at 13:09 UTC
    if i understand Test::Builder right, it constructs its own filehandles, so your redirects have no effect on outputs from Builder.pm. maybe you better work on the shell level.
Re: Redirecting STDOUT
by Moron (Curate) on Feb 12, 2007 at 16:00 UTC
    At the risk of taking too simplistic a view of your requirement, you can redirect the output of the whole perl program to log.txt and if desired STDERR to errlog.txt from the shell level using:
    myprog.pl > log.txt 2> errlog.txt
    or if ordinary and error output should be merged but appear in correct historic order...
    myprog.pl > log.txt 2>&1

    -M

    Free your mind

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://599451]
Approved by wfsp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-28 20:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found