develooper Front page | perl.perl5.porters | Postings from September 2001

Getting O.pm to not interfer with STDOUT

Thread Next
From:
Michael G Schwern
Date:
September 25, 2001 16:12
Subject:
Getting O.pm to not interfer with STDOUT
Message ID:
20010926011216.C627@blackrider
Given this program:

    BEGIN { open(FH, ">&STDOUT") or die $! }

I'm trying to make it work under B::Deparse with "quiet" mode on.

    perl -MO=-qq,Deparse -wle 'BEGIN { open(FH, ">&STDOUT") or die $! }'
    Bad file descriptor at -e line 1.
    BEGIN failed--compilation aborted at -e line 1.
    sub BEGIN {
        die $! unless open FH, '>&STDOUT';
    }

The problem is O.pm is closing STDOUT.  If instead we tie it to a
filehandle that just does nothing, it works, but I get a complaint
about the untie.

    perl -MO=-qq,Deparse -wle 'BEGIN { open(FH, ">&STDOUT") or die $! }'
    untie attempted while 2 inner references still exist at (eval 2) line 9.
    sub BEGIN {
        die $! unless open FH, '>&STDOUT';
    }

which I still can't figure out.


--- lib/O.pm	2001/09/25 21:18:29	1.1
+++ lib/O.pm	2001/09/26 05:07:29
@@ -8,7 +8,7 @@
     if ($options[0] eq '-q' || $options[0] eq '-qq') {
 	$quiet = 1;
 	open (SAVEOUT, ">&STDOUT");
-	close STDOUT;
+        tie *STDOUT, 'Tie::Dev::Null';
 	open (STDOUT, ">", \$O::BEGIN_output);
 	if ($options[0] eq '-qq') {
 	    $veryquiet = 1;
@@ -24,7 +24,8 @@
 
 	CHECK {
 	    if ($quiet) {
-		close STDOUT;
+		untie *STDOUT;
+                close STDOUT;
 		open (STDOUT, ">&SAVEOUT");
 		close SAVEOUT;
 	    }
@@ -47,6 +48,13 @@
     ];
     die $@ if $@;
 }
+
+
+package Tie::Dev::Null;
+require Tie::Handle;
+@ISA = qw(Tie::StdHandle);
+
+sub WRITE { 1 }
 
 1;
 



-- 

Michael G. Schwern   <schwern@pobox.com>    http://www.pobox.com/~schwern/
Perl6 Quality Assurance     <perl-qa@perl.org>	     Kwalitee Is Job One
That which stirs me, stirs everything.
	-- Squonk Opera, "Spoon"

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About