Question

I have a Perl script which reads three files and writes new files after reading each one of them. Everything is one thread.

In this script, I open and work with three text files and store the contents in a hash. The files are large (close to 3 MB).

I am using a loop to go through each of the files (open -> read -> Do some action (hash table) -> close)

I am noticing that the whenever I am scanning through the first file, the Perl terminal window in my Cygwin shell gets stuck. The moment I hit the enter key I can see the script process the rest of the files without any issues.

It's very odd as there is no read from STDIN in my script. Moreover, the same logic applies to all the three files as everything is in the same loop.

Has anyone here faced a similar issue? Does this usually happen when dealing with large files or big hashes?

I can't post the script here, but there is not much in it to post anyway. Could this just be a problem in my Cygwin shell?

If this problem does not go away, how can I circumvent it? Like providing the enter input when the script is in progress? More importantly, how can I debug such a problem?

sub read_set
{
@lines_in_set = ();
push @lines_in_set , $_[0];
while (<INPUT_FILE>) 
    {   $line = $_;
          chomp($line);
          if ($line=~ /ENDNEWTYPE/i or $line =~ /ENDSYNTYPE/ or eof())
          {
          push @lines_in_set , $line;
          last;
          }
          else 
          {
          push @lines_in_set , $line;       
          }
    }
    return @lines_in_set;
}

--------> I think i found the problem :- or eof() call was ensuring that the script would be stuck !! Somehow happening only at the first time. I have no idea why though

Was it helpful?

Solution

The eof() call is the problem. See perldoc -f eof.

eof with empty parentheses refers to the pseudo file accessed via while (<>), which consists of either all the files named in @ARGV, or to STDIN if there are none.

And in particular:

Note that this function actually reads a character and then "ungetc"s it, so isn't useful in an interactive context.

But your loop reads from another handle, one called INPUT_FILE.

It would make more sense to call eof(INPUT_FILE). But even that probably isn't necessary; your outer loop will terminate when it reaches the end of INPUT_FILE.

Some more suggestions, not related to the symptoms you're seeing:

Add

use strict;
use warnings;

near the top of your script, and correct any error messages this produces (perl -cw script-name does a compile-only check). You'll need to declare your variables using my (perldoc -f my). And use consistent indentation; I recommend the same style you'll find in most Perl documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top