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

Hi Monks, I am stuck with a weird error in my application. Not able to find why this is occurring. I will explain about my application architecture before explaining the error I am getting.

I am using set of cgi files for page mapping and html pages for view purpose. cgi file will pass all the arguements needed for a html file.

This works for all the pages expect one. In a particular page I am not able to print anything. The error I am getting when the control goes to this page is "readline() on closed filehandle GEN1". This error occurs in a page controller perl file. However, this is not occuring in other pages,but I have double checked that am opening and closing the filehandle in appropriate places. Any help is really appreciated. Below is the code snippet from which the error is occuring.

MAIN: { my $cgi = new CGI; my $uiutils = new UiUtils::; #User defined package my $path_info = $cgi->path_info(); $path_info =~ s/^\///; my $page; my $mypage; print qq|Content-type: text/html\n\n|; $page = $uiutils->getSkin(); chdir($LEGACY_CGI_DIR); my $code = qx(cat $LEGACY_CGI_DIR/$path_info); $0 = $path_info; use FileHandle; my $filehandle = new FileHandle; my $TMP_DIR = $::CONFIG->getTmpDirForUiController(); open ($filehandle, ">$TMP_DIR/$$"); select($filehandle); my $eval_error; { open(SAVEERR, ">&STDERR"); open(STDERR, ">/dev/null"); eval $code; $eval_error = $@; open(STDERR, ">&SAVEERR"); close(SAVEERR); } select(STDOUT); close($filehandle); my $error; if ($eval_error) { $error = "<span style='color:#ff0000'>EVAL ERROR: </span>" . $ +eval_error . "\n"; print STDERR "EVAL ERROR: $eval_error\n"; } open ($filehandle, "<$TMP_DIR/$$"); {undef $/; $mypage = <$filehandle>;} ########This is the line wher +e the error is occuring close($filehandle); unlink("$TMP_DIR/$$"); $mypage =~ /<title>(.*?)<\/title>/si; my $title = $1 || "Mprov"; print "<script>document.title='$title'</script>\n"; $mypage =~ s/.*<!--CONTENT-->//iso; $mypage =~ s/<!--ENDCONTENT-->.*//iso; $mypage =~ s/<img.*?\/images\/banner\.gif.*?>//iso; $mypage ||= $error; my $contents = new WebUi::Mech::RawHtml( -text => $mypage, ); $page->pack($contents); $page->showAll(); }

Replies are listed 'Best First'.
Re: readline() on closed filehandle GEN1
by Corion (Patriarch) on Jul 03, 2013 at 18:48 UTC

    You never check whether your

    open ($filehandle, "<$TMP_DIR/$$");

    is successfull.

    For debugging use

    my $tempfile= "$TMP_DIR/$$"; open ($filehandle, "<$tempfile") or die "Couldn't read tempfile '$tempfile': $!";

    Personally, I would look at using File::Temp instead for tempfiles.

      thanks for the quick reply Corion. One thing which I wonder about this file is that it works for all other pages expect one. Still I have debugged the way you told. Still getting the same error.