Solved

flock() on closed filehandle ?

Posted on 2002-07-27
7
1,361 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this tutorial I will aim to show you how simple is making a small application in WhizBase, how to add, remove and update data in the DB. I will make a small address book application where you can add, browse, update and remove addresses. I wi…
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now