in reply to Unbalance string table refcount

Consider the following:
use IO::File (); ## local $/; # you're not using this my $fh = IO::File->new(); # declare this just once foreach $file (sort keys %altnames) { my ($hr_title) = ( $file =~ m/(.*)\.txt/ ); print "<hr> <h1 align=center> $hr_title </h1>\n"; # don't need parens for print ... $fh->open( "$dir/$file" ) or die $!; # you gotta check! while (<$fh>) { print; } $fh->close; }
I think that having only one instance of IO::File will help (but I haven't tested this). Also, having something besides $_ as the "foreach" loop variable could make a difference, since you are also using $_ to iterate in the "while" loop, which might cause unexpected consequences (e.g. changing the strings that are used as the keys in %altnames).

update: I rigged up five dummy files with names as indicated in your error messages, and tested your original code with perl 5.8.0 on linux SuSE 7.3 -- didn't get any errors, so I'm at a loss. The comment about altering the hash key strings in the while loop was misguided; still, it seems better to have the outer loop using an iterator variable that's different from the inner loop. Anyway, my version ran as well (after I fixed a missing dash character in the "IO::File->new()" line).

Replies are listed 'Best First'.
Re: Re: Unbalance string table refcount
by Lhamo Latso (Scribe) on Mar 19, 2003 at 03:11 UTC
    Thanks so much.

    The thing that fixed the error appears to be using 2 levels of $_. I changed it to "foreach my $file (sort ... and the errors are gone.