develooper Front page | | Postings from July 2002

catching STDERR in test scripts on Win32

Amir Karger
July 1, 2002 21:03
catching STDERR in test scripts on Win32
Message ID:
Every test script I've seen until now (not including Games::Golf, since I
sent in a patch) has been unable to handle STDERR on Win32. I guess that's
because most folks work on Unix, which was true for me until a while ago.
(Hopefully this week we'll get the cable modem set up and be able to use
Linux again!) Anyway, it's actually not very hard to catch STDERR. I copied
the solution almost verbatim from `perldoc open`.

Basically, your test script should have something like this in it (I've
left in the old code for comparison):

    # Catching STDERR.
    my $IN  = "in.tmp";
    my $ERR = "err.tmp";

    foreach my $test ( @tests ) {
	# Prepare input file.
	open IN, ">$IN" or die $!;
	print IN $test->[0];
	close IN;

    # Special code for winMe
	    open(OLDERR, ">&STDERR");
	    close STDERR;
	    open(STDERR, ">$ERR") || die "Can't create new STDERR";

	    my $cmd = qq("$^X" $script $IN);
	    print "Running '$cmd':\t";
	    my $out = `$cmd`;
	    open(STDERR, ">&OLDERR");

	# Prepare command.
	#    my $cmd = qq("$^X" $script $IN 2>$ERR);
	#    print "Running '$cmd':\t";
	#    my $out = `$cmd`;

    # more testing...


This is how I changed the test script from tpr04a, and it worked like a
charm. It's also how Games::Golf now works.

The important part here is the open/close/open/close/open/close STDERR
stuff. The IN stuff is just how that particular test worked.

I would ask that the current refs, as well as the refs for all upcoming
contests, incorporate this into their tests. It's not very hard, and doesn't
hurt UNIX at all, but makes things much easier for those of us forced to
develop on Win32.


-Amir Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About