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

Hi guys, Ok first of all i am a complete novice(read dumb ass) when it comes to perl and i have to implement the following script. The issue that I am facing here is that files SLOG and ALLLOG are created in this script but when the script tries to read from these logs it gives an error: readline() on closed filehandle SLOG error. How do i fix this??? open ALLLOG, ">$ark2"; open SLOG, "$ark3"; $sline = readline SLOG;
  • Comment on readline on closed filehandle SLOG error

Replies are listed 'Best First'.
Re: readline on closed filehandle SLOG error
by cdarke (Prior) on Mar 10, 2010 at 09:11 UTC
    Please use <code> ... </code> tags around your code.

    The file handle probably failed to open. Always test your opens for failure, for example:
    open ALLLOG, ">$ark2" or die "Unable to open $ark2: $!"; open SLOG, "$ark3" or die "Unable to open $ark3: $!";

    The $! variable should give you the reason why it cannot open the file.
      And while preaching "Best practices" why not go all the way and do it as follows:
      open my $ALLLOG, '>', $ark2 or die "Unable to open $ark2: $!"; open my $SLOG, '<', $ark3 or die "Unable to open $ark3: $!";
      It employs lexical filehandles, uses the three arguiment version of open and does away with unnecessary double quotes.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James