Solved

flock() on closed filehandle ?

Posted on 2002-07-27
7
1,459 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

730 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