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

Hi there, could be I'm very tired today, but I cannot understand that behavior:

(I'm on Win XP64) when I run the following code it blocks at filehandle reading - while(my $ln = <DB>) - with the CPU working at full capacity.

I did some attempts in the hope of understand, I could only report that if I read the filehandle before enter the while cicle it works (as in the second example below), I checked the rest of the program for some strange interaction with that file and filehandle name (I tried unused FH name) but there is still something wrong.

Who have some hint? what could I have to check in the rest of the code? Thank you in advance.

The following code Blocks

FOR: foreach my $F($DB_PRI{FILE},$DB_OGGI{FILE}) { open(DB,"<$F") || &ERROR('CANNOT OPEN 1325'); while(my $ln = <DB>) { ...
The followings Works
FOR: foreach my $F($DB_PRI{FILE},$DB_OGGI{FILE}) { open(DB,"<$F") || &ERROR('CANNOT OPEN 1325'); my $line = <DB>; # this line frees the following while while(my $ln = <DB>) { ...

Replies are listed 'Best First'.
Re: Apparently strange beahavior that blocks at Filehandle reading
by 7stud (Deacon) on Jun 17, 2011 at 00:30 UTC
    I checked the rest of the program for some strange interaction with that file and filehandle name

    That's why you don't use bareword filehandles.

    use strict; use warnings; use 5.010; #Create some text files: my @fnames = ('test1.txt', 'test2.txt'); for my $fname(@fnames) { open my $OUT, '>', $fname or die "Couldn't open $fname: $!"; for my $num (1 .. 10) { say $OUT "$fname -- $num"; } } #------------------ sub print_error { say 'error'; } #------------------ #Read the text files: FOR: for my $fname('test1.txt', 'test2.txt') { open my $INPUT, '<', $fname or print_error(); while(my $line = <$INPUT>) { print $line; } print "\n"; } --output:-- test1.txt -- 1 test1.txt -- 2 test1.txt -- 3 test1.txt -- 4 test1.txt -- 5 test1.txt -- 6 test1.txt -- 7 test1.txt -- 8 test1.txt -- 9 test1.txt -- 10 test2.txt -- 1 test2.txt -- 2 test2.txt -- 3 test2.txt -- 4 test2.txt -- 5 test2.txt -- 6 test2.txt -- 7 test2.txt -- 8 test2.txt -- 9 test2.txt -- 10

    When the input line operator, <>, reads end-of-file, it returns a false value that causes the while loop to end.

    How about using the 3-arg form of open()?

    open(DB,"<$F") #compare to: open my $INFILE, '<', $fname or die "Couldn't open $fname: $!"

    How about printing out the filename in the loop?

      Sorry, I misguded all of you and myself.

      I blamed the filehandle handling for that behaviour, but it has nothing to do with the problem, at least not that filehandle, problem is about the STDOUT, in fact I tested the code through the shell awaiting for some print on STDOUT during the cicle, but due to a long operation inside the loop the unflushed output gave me NO feedback; setting the $|=1; give me each cicle outpup in real time...

      however, thanks to all readers

      On the other hand now I have the problem about unexpected slownees of the following code: (don't care about reply, I only post it here for completness, but more rightly I'll possibly send a new specific post for it)

      $DB_STATS{COMB} is an hash of about other 180000 hashes each one with 2 keys, {Rit} and {Usc}. (COMB keys {$C} are of about 20-25 bytes)

      foreach my $C( keys(%{$DB_STATS{COMB}}) ) { $DB_STATS{COMB}{$C}{Rit}++; }
        $DB_STATS{COMB} is an hash of about other 180000 hashes each one with 2 keys, {Rit} and {Usc}. (COMB keys {$C} are of about 20-25 bytes)

        Could you post the first 10 lines produced by:

        use Data::Dumper; ... dumper( \%DB_STATS );

        ?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Apparently strange beahavior that blocks at Filehandle reading
by Anonymous Monk on Jun 17, 2011 at 04:14 UTC
    what could I have to check in the rest of the code?

    Copy your working file, and start deleting as much code as possible, until you're left with very short program, which demonstrates the bug, as per How do I post a question effectively?

    Longshot, see DB and perldebguts

      Thank you for your advices I'll profit them... I really know not much on perl debugging tools...

      ( how you could imagine I only posted that to restrict the scope of the problem to some notionism I could be missing, in the aim to reduce efforts and time spent by the interested monks on that problem )

      Thank you all again!
Re: Apparently strange beahavior that blocks at Filehandle reading
by Marshall (Canon) on Jun 17, 2011 at 08:15 UTC
    I would suggest printing out the names of the two files. Then from command line, see if you can ">type filename". Maybe these are binary files?
      No, they are pure text files; however I discover the problem, but I didn't understand what you mean and its purpose... I'll be interested if you'll explain it.

      Thank you