Solved

flock() on closed filehandle ?

Posted on 2002-07-27
7
1,416 Views
Last Modified: 2013-12-25
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.
0
Comment
Question by:sayhi
  • 3
  • 2
  • 2
7 Comments
 
LVL 1

Author Comment

by:sayhi
ID: 7181700
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)
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7181814
your script closes the file on line 100
0
 
LVL 5

Expert Comment

by:Peewee
ID: 7184841
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);
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 1

Author Comment

by:sayhi
ID: 7186399
"...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! =)
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7186466
.. this all ends up in short words (see my very first comment :-)
0
 
LVL 5

Accepted Solution

by:
Peewee earned 50 total points
ID: 7187468
sayhi,

flock() on closed filehandle GetNews::TITLEFILE at /home2/blah/public_html/cgi-bin/GetNews/GetNews.pm line 105

what does this tell you?


'TITLEFILE' no longer exists at this point, and therefore is unable to complete and file locking process - hence the error.


your problem:
short solution take the offending code:
flock(TITLEFILE,8); << line 105
close(TITLEFILE);"

extra:
take a look at your code, and ask your yourself why you have a filehandle open when it has already benn closed, ie is this extra processing that has been deleted by mistake or is it because code was duplicated elsewhere by accident.

let me know if you need more help.

regards
Peewee
0
 
LVL 5

Expert Comment

by:Peewee
ID: 7190048
sayhi,

glad to be of help..

Peewee
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to dynamically set the form action using jQuery.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question