use Tie::File;

my $o = tie my @x,"Tie::File","myDataFile"; #tie the file and 
                # create object
$o->flock();	#waits till we get the lock and then locks

$y = $x[0];	#reads from the file, may leave the value in the cache

#....
#...
#more code

$x[0]++;    #we know that no other user can have updated since we
            # had the file locked, we are clean and safe
