The problem is not in closing fd 0

Let's look at the following sample:

# dup.pl
open my $fh, '<&=',0 or die;
print fileno STDIN, ":", scalar readline STDIN;
close STDIN;
print fileno $fh, ":", scalar readline $fh;

########
bash $ { echo 1; sleep 1; echo 2; } | perl dup.pl 
0:1
0:2 

(During this I've investigated, that if we do echo -ne "1\n2\n" | perl dup.pl, 
then <$fh> receive nothing. I think that's because of buffered read. Don't know 
should be this considered a bug)

Then we add the same 

open my $fh, '<&=',0 or die;

to the beginning of the example and check the fileno at the end

0 == fileno $fh or die "fileno fh nonzero";

then we will see, that fileno 0 was kept, it still assigned to stdin (this 
could be checked additionally), but the behavior of this program still the 
same: we got an output of PerlIO.pm

On 08.12.2011, at 21:18, Father Chrysostomos via RT wrote:

> On Thu Dec 08 03:25:08 2011, [email protected] wrote:
>> #!/usr/bin/perl
>> use strict;
>> use warnings;
>> 
>> my $data = "aaa\nbbb\nccc\n";
>> 
>> # Ok
>> close STDIN or die $!;
>> open( STDIN, '<', \$data ) or die $!;
>> @ARGV = ();
>> while (<STDIN>)
>> {
>>    print "[[$ARGV]]***$_";
>> }
>> 
>> # Bug
>> close STDIN or die $!;
>> open( STDIN, '<', \$data ) or die $!;
>> @ARGV = ();
>> while (<>)
>> {
>>    # print PerlIO.pod
>>    print "[[$ARGV]]***$_";
>> }
> 
> By closing STDIN you are closing fd 0, which is reused by the next file
> opened.
> 
> I seem to remember this came up about three years ago and the consensus
> was that it is not a bug.  I’m not sure.
> 
> If my memory serves me correctly, searching for ‘you passed sub
> protections’ and ‘This gotta change’ in the archives will bring it up.
> 
> -- 
> 
> Father Chrysostomos
> 

-- 
Moscow.pm mailing list
[email protected] | http://moscow.pm.org

Ответить