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

Dear all. I'm a novice perl user. I'm using perl to read a large amount of data but not overly large. So I'm confused why it's hanging. By hanging I mean just sitting there. The state is "S" or sometimes "R" Everywhere I read says to use while ($line = <FILE>) and I did that from the start but still not working. Here is my code:

open (FIND_OUTPUT , "$CT find . -version'brtype($branch)' -print -all +|"); # I only tried to flush the buffer because it's hanging # But still doesn't work select((select(FIND_OUTPUT), $|=1)[0]); while ($line = <FIND_OUTPUT>) { ($filename = $line) =~ s/(.*)@@.*/\1/; ($version = $line)=~ s/(.*)@@(.*)/\2/; #... do a bunch of line processing and put line in hash $ccase_filehash{$filename} .= ";" . $version; } close FIND_OUTPUT; foreach $key (keys %ccase_filehash) { print "$key: $ccase_filehash{$key} \n"; }

When that failed then I tried to write the output to a file instead. That works, the file gets written correctly and it's only 800 KB. But then it hangs either processing the file or printing out the hash

system ("$CT find $vob -version 'brtype($branch)' -print $all > /tmp/f +ind.out"); open (FIND_OUTPUT , "/tmp/find.out"); # This doesn't help but I put it in anyway select((select(FIND_OUTPUT), $|=1)[0]); while ($line = <FIND_OUTPUT>) { ($filename = $line) =~ s/(.*)@@.*/\1/; ($version = $line)=~ s/(.*)@@(.*)/\2/; #... do a bunch of line processing and put line in hash $ccase_filehash{$filename} .= ";" . $version; } foreach $key (keys %ccase_filehash) { print "$key: $ccase_filehash{$key} \n"; }

I should note that when the output of the CT command is smaller then the script doesn't hang. I also tried this without the "select" line to flush the buffer and also didn't work.

Replies are listed 'Best First'.
Re: hanging script on read
by stevieb (Canon) on Aug 16, 2015 at 02:02 UTC

    Obligatory... please always put use strict;, use warnings; at the top of your scripts, and here's the idiomatic and recommended three-arg open with lexical file-handles:

    # read open my $fh, '<', 'file.ext' or die $!; # write open my $wfh, '>', 'file.ext' or die $!;

    Peppering the code with print statements to try to find out where the code is 'hanging' might help as well.

    To get really nitty gritty once the above have been done, throw in a $DB::single = 1; just above where you think the code is hanging, then, on the command line:

    # errata removed $ perl -d # 'continue' and stop at your breakpoint with 'c' main::( $DB::single = 1; DB<1> c # step into your variable with 's' main::( my $x = 'y'; DB<1> s # display to STDOUT the value of the variable with "x $var" main::( print $x; DB<1> x $x 0 'y'

    If it hangs above a print or in debugging your breakpoint, you'll know exactly where to focus your attention.

      Thank you stevie b. I will try to put the use strict; use warnings; at top. Also I know where it hangs. I have used print. In the first code snippet it hangs in the while loop processing the output of the ct find command In the second code snippet it hangs while printing out the hash, e.g. processing the hash in the foreach loop That's why I tried to flush the buffer. So it's unclear to me because it's not like its a super huge amount of data. thank you for your help.

Re: hanging script on read
by 1nickt (Canon) on Aug 16, 2015 at 01:07 UTC

    You should always check the return value of open(). It may not be working.

    And it looks like you might like File::Find::Rule.

    The way forward always starts with a minimal test.
Re: hanging script on read
by betterworld (Curate) on Aug 16, 2015 at 09:49 UTC

    Can you provide a short script that we can test? Right now we cannot test your code because $CT and brtype and some more things are unknown.

      I'm not sure how to provide that. The ct command is what returns the data from our DB. Let me think about it.