[perl #92728] open.pm without :std should leave std alone
authorFather Chrysostomos <sprout@cpan.org>
Fri, 16 Sep 2011 03:22:28 +0000 (20:22 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Fri, 16 Sep 2011 04:33:18 +0000 (21:33 -0700)
‘use open’ without :std was turning off layers on STDIN and STDOUT
(but not STDERR), due to a few missing if() conditions and due to an
omission of STDERR (which probably also caused STDERR’s handles to
accumulate through multiple calls to ‘use open ':std'...’).

This fixes that.  (As if you were expecting otherwise.)

lib/open.pm
lib/open.t

index 1bfe0d6..d3f2d1b 100644 (file)
@@ -1,7 +1,7 @@
 package open;
 use warnings;
 
-our $VERSION = '1.08';
+our $VERSION = '1.09';
 
 require 5.008001; # for PerlIO::get_layers()
 
@@ -95,16 +95,22 @@ sub import {
            }
        }
        if ($type eq 'IN') {
-           _drop_oldenc(*STDIN, @val);
+           _drop_oldenc(*STDIN, @val) if $std;
            $in  = join(' ', @val);
        }
        elsif ($type eq 'OUT') {
-           _drop_oldenc(*STDOUT, @val);
+           if ($std) {
+               _drop_oldenc(*STDOUT, @val);
+               _drop_oldenc(*STDERR, @val);
+           }
            $out = join(' ', @val);
        }
        elsif ($type eq 'IO') {
-           _drop_oldenc(*STDIN,  @val);
-           _drop_oldenc(*STDOUT, @val);
+           if ($std) {
+               _drop_oldenc(*STDIN, @val);
+               _drop_oldenc(*STDOUT, @val);
+               _drop_oldenc(*STDERR, @val);
+           }
            $in = $out = join(' ', @val);
        }
        else {
index f9cacab..586d8e2 100644 (file)
@@ -7,7 +7,7 @@ BEGIN {
        require './test.pl';
 }
 
-plan 23;
+plan 24;
 
 # open::import expects 'open' as its first argument, but it clashes with open()
 sub import {
@@ -195,6 +195,22 @@ SKIP: {
         "test for an endless loop in PerlIO_find_layer");
 }
 
+is runperl(
+     progs => [
+        'use open q\:encoding(UTF-8)\, q-:std-;',
+        'use open q\:encoding(UTF-8)\;',
+        'if(($_ = <STDIN>) eq qq-\x{100}\n-) { print qq-stdin ok\n- }',
+        'else { print qq-got -, join(q q q, map ord, split//), "\n" }',
+        'print STDOUT qq-\x{ff}\n-;',
+        'print STDERR qq-\x{ff}\n-;',
+     ],
+     stdin => "\xc4\x80\n",
+     stderr => 1,
+   ),
+   "stdin ok\n\xc3\xbf\n\xc3\xbf\n",
+   "use open without :std does not affect standard handles",
+;
+
 END {
     1 while unlink "utf8";
     1 while unlink "a";