Subject: | Persistent filehandle problems? |
Distribution: ParseLex-2.15
Perl Version: 5.005_03
5.6.1
OS: Red Hat 6.2 (Linux 2.2.14-5.0 i686)
Gentoo 1.3 (Linux 2.4.19-gentoo-r7 i686)
It appears that ParseLex is holding onto old file handles or something
like that. A patch for the problem is attached. The code below produces
the problem.
#!/usr/bin/perl
use strict;
use Symbol;
my $file = "/tmp/showbugtest.dat";
open F, ">$file" || die "Can't open test data file '$file' $!";
while ( <DATA> ) {
print F;
}
close F;
my $fh = gensym;
my_parser::parse($file); # Prints: 'GOT: 3 lines'
my_parser::parse($fh); # No output because of bad file handle
my_parser::parse($file); # Prints: 'GOT: 0 lines', but should be the
# same as the first call to parse
package my_parser;
use strict;
use Parse::Lex;
use Symbol;
sub parse {
my $file = shift;
my $fh;
my $should_close = 0;
if ( ref $file eq "GLOB" ) {
$fh = $file;
fileno( $fh ) or return undef;
}
else {
$fh = gensym;
open $fh, $file or die "Can't open file '$file' for import. Error: $!";
$should_close = 1;
}
my $token;
my $line_cnt = 0;
my $lexer = Parse::Lex->new( EOR => '\n', LINE => '.*' );
$lexer->from($fh);
while ( $lexer->nextis(\$token) ) {
++ $line_cnt if $token->name eq "EOR";
}
close $fh if $should_close; # take this out and it MAY work
print "GOT: $line_cnt lines\n";
}
__END__
1,one
2,two
3,three
Index: CPAN/ParseLex/lib/Parse/ALex.pm
diff -c CPAN/ParseLex/lib/Parse/ALex.pm:1.1 CPAN/ParseLex/lib/Parse/ALex.pm:1.2
*** CPAN/ParseLex/lib/Parse/ALex.pm:1.1 Thu Dec 5 15:52:11 2002
--- CPAN/ParseLex/lib/Parse/ALex.pm Thu Dec 5 16:01:59 2002
***************
*** 252,260 ****
my $self = shift;
my $debug = 0;
# From STREAM
! local *X = $_[0];
! print STDERR "arg: $_[0] ", fileno(X) , "\n" if $debug;
! if (defined(fileno(X))) {
$self->[$STREAM] = $_[0];
print STDERR "From stream\n" if $debug;
--- 252,259 ----
my $self = shift;
my $debug = 0;
# From STREAM
! print STDERR "arg: $_[0] ", fileno($_[0]) , "\n" if $debug;
! if (defined(fileno($_[0]))) {
$self->[$STREAM] = $_[0];
print STDERR "From stream\n" if $debug;