prefectuous has asked for the wisdom of the Perl Monks concerning the following question:

This is perl, v5.8.8 built for cygwin-thread-multi-64int

Please forgive the size of the test example that is included. It is as small as I could make it and still demonstrate the apparent bug.

When working with __DATA__ in conjunction with sockets, the first read of the filehandle breaks. If I tell() the filehandle the tell resolves to the wrong location in the file (EXAMPLE 1), if I don't tell(), the filehandle reads twice. Any subsequent call to the filehandle works properly. (EXAMPLE 2)

###### EXAMPLE 1 $ telnet localhost 7171 Trying 127.0.0.1... ) ; our $T ; use strict ; 1; __DATA__ SELECT * FROM blart ; _______________________ ) ; our $T ; use strict ; 1; __DATA__ SELECT * FROM blookie ; Connection closed by foreign host. ######## EXAMPLE 2 $ telnet localhost 7171 Trying 127.0.0.1... SELECT * FROM blart ; _______________________ SELECT * FROM blookie ;
TEST CODE FOLLOWS:

#! /usr/bin/perl -w use Test::Server ; my $S = Test::Server->new( max_servers => 4, max_requests => 100, reverse_lookups => 'off', background => 1, host => '127.0.0.1', proto => 'TCP', port => 7171 ) ; $S->run(); package Test::Server; use Test::Bar ; use Net::Server::PreForkSimple; @ISA = qw(Net::Server::PreForkSimple); use strict ; sub process_request { my $A = Test::Bar->new(table => "blart") ; my $B = Test::Bar->new(table => "blookie") ; print $A->output() ; print "_______________________\n\n" ; print $B->output() ; } 1 ; package Test::Bar ; use Exporter ; our @ISA = qw(Exporter) ; use Test +::Foo ; push @ISA, qw(Test::Foo) ; our @EXPORT = qw($T) ; our $T ; use strict +; 1; __DATA__ SELECT * FROM <TMPL_VAR NAME=table> ; package Test::Foo ; use Exporter ; use HTML::Template ; our @ISA = qw(Exporter) ; sub new { my $class = shift ; my %foo = @_ ; my $Tref ; # reference to a scalar containing a Template my $useblock = 'use ' . $class . ' qw($T);' ; eval $useblock ; my $refblock = '$Tref = \$' . $class . '::T;' ; eval $refblock ; my $self = \%foo ; bless($self,$class) ; $self->{'__TEMPLATE__'} = $Tref ; my $preload = $self->loadfh() ; return $self ; } sub loadfh { my $self = shift ; my $Tref = $self->{'__TEMPLATE__'} ; return 1 if ($$Tref =~ /\w+/) ; my $class = ref($self) ; my $fh ; my $localDATA = $class . '::DATA'; my $fhblock = '$fh = ' . '*' . $localDATA . ';' ; eval($fhblock); tell($fh) ; while(<$fh>) { $$Tref .= $_ ; } return 0 ; } sub output { my $self = shift ; my $Tref = delete $self->{'__TEMPLATE__'} ; my $t = HTML::Template->new_scalar_ref($Tref, die_on_bad_params => + 0); $t->clear_params() ; $t->param(%$self) ; $self->{'__TEMPLATE__'} = $Tref; return $t->output(); } sub fields { my $self = shift ; my $Tref = delete $self->{'__TEMPLATE__'} ; my $t = HTML::Template->new_scalar_ref($Tref, die_on_bad_params => + 0); $t->clear_params() ; my @f = $t->param() ; $self->{'__TEMPLATE__'} = $Tref; return @f ; } 1 ;

Replies are listed 'Best First'.
Re: Bug in __DATA__ filehandle
by chromatic (Archbishop) on Nov 01, 2008 at 19:09 UTC

    Your code isn't sufficiently obvious to me to see the flow of control and what you expect (nor do I understand how you're using __DATA__, but if you have a filehandle open in a parent and a child simultaneously (that is, if you've opened it in a parent and access it in a child), they share the same position in the file. Re-open the filehandle in the children before doing anything which will affect the file position, or read the data before forking off any kids.

      Test::Bar is a template, which inherits Test::Foo.

      Test::Foo reaches into Test::Bar and sticks everything from Test::Bar::__DATA__ into $Test::Bar::T at constructor time. Subsquent objects evaluate $Test::Bar::T for content and should never touch __DATA__.

      So no matter what, there should only ever be one access of the __DATA__ handle. The bug is kindof chicken and egg. If I warn from the function right before the filehandle read, the warning only occurs once, confirming that Test::Foo::loadfh() is only called once, yet the data stored in $Test::Bar::T apparently changes after the second socket connection. So something is getting called twice, but it is not function that is reading the filehandle.

      When I was tweaking, I did a: warn(tell($fh)), and it reported a negative file index. I am assuming that shouldn't happen. Can anyone confirm that a tell() should not report a negative file index?