develooper Front page | perl.perl5.porters | Postings from December 2017

Re: [perl #130071] open "-" doesn't use STDIN

Thread Previous
From:
Lukas Mai
Date:
December 19, 2017 08:22
Subject:
Re: [perl #130071] open "-" doesn't use STDIN
Message ID:
5b575daf-3079-9af7-f4f0-f1927c1b058a@web.de
Am 17.12.2017 um 02:19 schrieb Zefram via RT:
> After closing one of the standard I/O streams, it is normal that a
> subsequent open will accidentally reuse the file descriptor.  The kind
> of aliasing seen here is to be expected from this sort of activity,
> and is not a bug.  This ticket should be closed.

This is unrelated to file descriptors:

% perl -we 'close STDIN; open my $fh, q{<}, \qq{foo\nbar\n}; open my 
$fh2, q{<-}; print scalar readline $fh; print scalar readline STDIN; 
print scalar readline $fh2'
foo
readline() on closed filehandle STDIN at -e line 1.
Use of uninitialized value in print at -e line 1.
bar

I.e. you get the same output if $fh refers to a string and doesn't use a 
file descriptor internally. $fh2 still somehow aliases the stream.

Worse:

% perl -we 'close STDIN; open my $fh, q{<}, \qq{foo\nbar\n}; open STDIN, 
q{echo hi |}; open my $fh2, q{<-}; print scalar readline $fh; print 
scalar readline STDIN; print scalar readline $fh2; print fileno STDIN, 
qq{\n}'
foo
hi
bar
0

Now we have a real STDIN handle, it's bound to file descriptor 0, and 
still $fh2 refers to $fh, not STDIN.

-- 
Lukas Mai <l.mai@web.de>

Thread Previous


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