LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] Perl file locking

[ILUG] Perl file locking

Fergal Daly fergal at esatclear.ie
Wed Jul 25 19:06:15 IST 2001


This isn't the source of your problem but you shouldn't be using 2 and 8 or
any other hard coded OS dependent value as there is no guarantee that they
are the correct values and they're also completely unintuitive. Do this

use Fcntl ':flock';

flock(FILE, LOCK_EX());   
flock(FILE, LOCK_UN());

etc.  

Also, what is the seek doing, open >> will position you at the end of the
file so the seek seems a bit odd. It's probably a bad idea to have the seek
in a function called lock as it doesn't just lock. Assuming you ditch the
seek you now have 2 functions which don't do anything except call a function
and on top of that, they are locked into calling it a specific file handle,
so you can't use them for any other file. I think you'd be better of to just
call flock directly when you need it.

You also seem to have a hash called %items whose keys are numbers, starting
from 1. This would be much better done as an array.

You're actually locking the files before opening them, the flock function
works on a *file handle* and a file handle only has a value *after* the file
has been opened.

Finally, you don't lock the logfile, I presume you do actually want to do
that too.

Anyway, none of the above are likely to be the cause of your problem. In
fact I'd say it has nothing to do with locking at all. If you take out all
the locking code, you'll probably find it doesn't write anything out either.

You're not checking the return codes of anything, this is a bad idea. You
need to replace all your

open(FILE, "blah");

with 

open(FILE, "blah") or die "Couldn't open blah, $!";

flock(FILE, LOCK_EX());

with

flock(FILE, LOCK_EX()) or die "Couldn't lock file, $!";

and maybe even check the return codes from close too (this is especially
important if the file handle is from a piped command rather than just a
file).

Now you'll know for definite whether you have a permissions problem.

By the way, I notice your files are in /, which means you're probably
running this script as root. / is not a good place to store files and
running dodgy Perl scripts as root is a pretty bad idea!

One thing that might be it is

while ( $item{$i} gt " ")

this is a stringwise comparison of something with " ", this will only be
true for strings who's first character has a higher ascii value that the
space character. Is this what you meant? This will be true for almost every
sensible string you can think of, so in theory that loop will execute lots
of times.

Also, are you using " " as a sentinel value or something? This sort of thing
is rarely needed in Perl as arrays, string and hashes know how big they are
you don't need to do stuff like incrementing $i until you hit a special
value that marks the end. Have a look at the foreach statement

Another thing is that maybe you did actually use an array rather than a
hash elsewhere in your code for items in which case you need to change { and
} to [ and ].

If this is the problem and you would have spotted it straight away if you
had done a use strict at the start of your program. Everyone should use
strict (almost) everywhere, it can be a bit of a pain initially but it helps
prevent so many hard to find errors that it's well worth it.

Finally run the program with perl -w, this will give you warnings about all
sorts of things you're doing that are valid Perl but probably wrong. Then
change your program until Perl doesn't give any more warnings, that way when
you see a warning, you know it's something to worry about.

If you follow all the advice above you might find the bug half way through.
Don't stop following the advice, this is all standard stuff, any book on
Perl will tell you to do the same and some people won't even begin to help
if you're script doesn't start with

#! /usr/bin/perl -w

use strict;

Hope some of this helps,

Fergal



On Wed, Jul 25, 2001 at 04:58:08PM -0000, frank.05 at neo.tamu.edu wrote:
> I have a problem with one of my scripts.
> I have tried to lock a datafile but when when the script is executed there is no data saved on the specified files.
> Can anyone help me out? i have checked my permisions and evrything is set ok.
> 
> here is part of the script
> 
> sub lock 
>    {
> 	flock (DATAFILE, 2);
> 	seek (DATAFILE, 0, 2);
>    }
> sub unlock
>    {
> 	 flock (DATAFILE, 8);
>    }
>  
> sub write_data
>    {
> 	$i=1; 
>  open(DATAFILE, ">>/data.txt");
>  open(LOG_OUT,">>/data.log");
>  print LOG_OUT "$response{$i}:";
>    $i++;    
>  while ( $item{$i} gt " ")
>        {
>          print DATAFILE "$response{$i}:";
>          print LOG_OUT  "$response{$i}:";
> 	 $i++;
>        }
> 
> print LOG_OUT "\n";
>    close(DATAFILE);
>    close(LOG_OUT);   
>        }
> 
> lock(DATAFILE);
>  &write_data;
> unlock(DATAFILE);
> 
> 
> -- 
> Irish Linux Users' Group: ilug at linux.ie
> http://www.linux.ie/mailman/listinfo/ilug for (un)subscription information.
> List maintainer: listmaster at linux.ie

-- 




More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell