Link to home
Start Free TrialLog in
Avatar of sayhi
sayhi

asked on

flock() on closed filehandle ?

Hello,
I'm having trouble with a perl script. I do not understand what the error means. This is what it says in my error log:
flock() on closed filehandle GetNews::TITLEFILE at /home2/blah/public_html/cgi-bin/GetNews/GetNews.pm line 105.

here's the trouble section in the script:
sub print_title
{
  shift;
  my $site_name=shift;
  my $file_desc=shift;
  my $site_url=shift;
 
  my $title_file=$html_template_path."/title.template";
  my $title="";
  open (TITLEFILE,"<$title_file") ||die "Cannot find title template file";
  flock(TITLEFILE,2);
  while(<TITLEFILE>)
  {
    $title=$title.$_;
  }
  flock(TITLEFILE,8);
  close(TITLEFILE);
  $title =~ s/SITEURL/$site_url/g;
  $title =~ s/SITENAME/$site_name/g;
  $title =~ s/FILEDESC/$file_desc/g;
  print $title;
  flock(TITLEFILE,8); << line 105
  close(TITLEFILE);
}

Any explainations/suggestions appreciated.
Avatar of sayhi
sayhi

ASKER

side thing (i don't know if it has to do with the error), Readme file said something like "you may need to 'chmod o+rx' the two .pm files and the directory".
can anyone explain what o+rx translates to? (all i know what to with chmod is with numbers like 777, 775, 644, etc)
your script closes the file on line 100
sayhi,
here's an explanation of whats going in your script:

the code below basically opens a filehandle 'TITLEFILE', provides it with exclusive access, stores the contents of the file into a variable, unlocks the file and closes the file handle 'TITLEFILE'.

============
open (TITLEFILE,"<$title_file") ||die "Cannot find title template file";
 flock(TITLEFILE,2);
 while(<TITLEFILE>)
 {
   $title=$title.$_;
 }
 flock(TITLEFILE,8);
 close(TITLEFILE);
==================

the code below basically performs regular expressions on the contents of the file now stores in the variabale and prints the variable out, this is more than likely a template with html content and attempts to unlock and close the filehandle 'TITLEFILE'.  This will not be successful as 'TITLEFILE' no longer exists at this point, and therefore is unable to complete and file locking process.

===========================
$title =~ s/SITEURL/$site_url/g;
 $title =~ s/SITENAME/$site_name/g;
 $title =~ s/FILEDESC/$file_desc/g;
 print $title;
 flock(TITLEFILE,8); << line 105
 close(TITLEFILE);


solution:
the code below is redundant in your script, either take it out or check that you have not missed some other code that should be present and accompanies this code.

let me know how it goes

regards Peewee
flock(TITLEFILE,8); << line 105
 close(TITLEFILE);
Avatar of sayhi

ASKER

"...attempts to unlock and close the filehandle 'TITLEFILE'.  This will not be successful as 'TITLEFILE' no longer exists at this point, and therefore is unable to complete and file locking process."

okay, I understand that. thanks for explaining.

Though could you explain what is this error telling me?:
flock() on closed filehandle GetNews::TITLEFILE at /home2/blah/public_html/cgi-bin/GetNews/GetNews.pm line 105

Is it saying that where flock() is on line 105, there is nothing to close? or I have to do what you did, go look at the code and see what the problem is. Just wondering, since I want to understand what it means if I ever come across this error again.

"solution:
the code below is redundant in your script, either take it out or check that you have not missed some other code that should be present and accompanies this code.

let me know how it goes

regards Peewee
flock(TITLEFILE,8); << line 105
close(TITLEFILE);"

I took it out. It worked! =)
.. this all ends up in short words (see my very first comment :-)
ASKER CERTIFIED SOLUTION
Avatar of Peewee
Peewee

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sayhi,

glad to be of help..

Peewee