develooper Front page | perl.perl5.porters | Postings from July 2008

Re: Fun with open() on a string

Thread Previous | Thread Next
Mark Mielke
July 30, 2008 07:12
Re: Fun with open() on a string
Message ID:
Graham Barr wrote:
>> Peter Scott wrote:
>>> This very nearly qualifies as an easter egg.  Inspect the following:
>>>    perl -e 'close STDIN; open STDIN, "<", \$x; print <>'
> Yes. <> is reading from stdin in C (file descriptor 2) which will be 
> what was first opened after closing STDIN.
> The open STDIN, "<", \$x causes the PerlIO module to be loaded, so the 
> first file opened is
> What you see in the output is what is after the __END__ in

I would have been more impressed if it WAS an easter egg... Hahaha...

Thanks for the explanation, Graham.


P.S. This sort of issue has bit me before. On some systems, init runs 
/etc/inittab entries without an STDIN, STDOUT, or STDERR. This leads to 
many issues as these file descriptors are inevitable assigned some 
important, and if the script assumes STDIN, STDOUT, or STDERR do exist, 
and do something with them, or close them...

Mark Mielke <>

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