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

Re: Fun with open() on a string

Thread Previous | Thread Next
From:
H.Merijn Brand
Date:
July 30, 2008 07:54
Subject:
Re: Fun with open() on a string
Message ID:
20080730165433.50d6b0da@pc09.procura.nl
On Wed, 30 Jul 2008 08:08:38 -0500, Graham Barr <gbarr@pobox.com> wrote:

> 
> On Jul 29, 2008, at 8:42 AM, Mark Mielke wrote:
> 
> > Peter Scott wrote:
> >> This very nearly qualifies as an easter egg.  Inspect the following:
> >>
> >>    perl -e 'close STDIN; open STDIN, "<", \$x; print <>'
> >>
> >> Guess what it does.  Run it.  See if you were right.
> >>
> >> Bugworthy?  I don't know; maybe, that's why I mention it here.  But  
> >> it had
> >> some entertainment value for me today.
> >>
> >
> > +1 for weird. :-)
> >
> > Do you have an explanation for why it does what it does? :-)
> 
> 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 PerlIO.pm
> 
> What you see in the output is what is after the __END__ in PerlIO.pm

BTW your explanations is fine on most systems, at least those that allow
you to open files on file descriptor 0.

$ perl -e 'close STDIN; eval "use CPAN::Debug"; print <>'

=head1 LICENSE

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=cut

$ perl -e 'close STDIN; eval "use AutoSplit"; print <>'

# test functions so AutoSplit.pm can be applied to itself:
sub test1 ($)   { "test 1\n"; }
sub test2 ($$)  { "test 2\n"; }
sub test3 ($$$) { "test 3\n"; }
sub testtesttesttest4_1  { "test 4\n"; }
sub testtesttesttest4_2  { "duplicate test 4\n"; }
sub Just::Another::test5 { "another test 5\n"; }
sub test6       { return join ":", __FILE__,__LINE__; }
package Yet::Another::AutoSplit;
sub testtesttesttest4_1 ($)  { "another test 4\n"; }
sub testtesttesttest4_2 ($$) { "another duplicate test 4\n"; }
package Yet::More::Attributes;
sub test_a1 ($) : locked :locked { 1; }
sub test_a2 : locked { 1; }
$

will show you that you can play with this knowledge. You don't even
need the open.

-- 
H.Merijn Brand          Amsterdam Perl Mongers  http://amsterdam.pm.org/
using & porting perl 5.6.2, 5.8.x, 5.10.x, 5.11.x on HP-UX 10.20, 11.00,
11.11, 11.23, and 11.31, SuSE 10.1, 10.2, and 10.3, AIX 5.2, and Cygwin.
http://mirrors.develooper.com/hpux/           http://www.test-smoke.org/
http://qa.perl.org      http://www.goldmark.org/jeff/stupid-disclaimers/

Thread Previous | 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