LockedFile(3pm) | User Contributed Perl Documentation | LockedFile(3pm) |
IO::LockedFile - supply object methods for locking files
use IO::LockedFile; # create new locked file object. $file will hold a file handle. # if the file is already locked, the method will not return until the # file is unlocked my $file = new IO::LockedFile(">locked1.txt"); # when we close the file - it become unlocked. $file->close(); # suppose we did not have the line above, we can also delete the # object, and the file is automatically unlocked and closed. $file = undef;
In its simplistic use, the IO::LockedFile class gives us the same interface of the IO::File class with the unique difference that the files we deal with are locked using the Flock mechanism (using the "flock" function).
If during the running of the process, it crashed - the file will be automatically unlocked. Actually - if the IO::LockedFile object goes out of scope, the file is automatically closed and unlocked.
So, if you are just interested in having locked files with "flock", you can skip most of the documentation below.
If, on the other hand, you are interested in locking files with other schemes then Flock, or you want to control the behavior of the locking (having non blocking lock for example), read on.
Actually the class IO::LockedFile is kind of abstract class.
Why abstract? Because methods of this class call the methods "lock" and "unlock". But those methods are not really implemented in this class. They suppose to be implemented in the derived classes of IO::LockedFile.
Why "kind" of abstract? Because the constructor of this class will return an object!
How abstract class can create objects? This is done by having the constructor returning object that is actually an object of one of the derived classes of IO::LockedFile.
So by default the constructor of IO::LockedFile will return an object of IO::LockedFile::Flock. For example, the following:
use IO::LockedFile; $lock = new IO::LockedFile(">bla"); print ref($lock);
Will give:
IO::LockedFile::Flock
So what are the conclusions here?
First of all - do not be surprised to get object of derived class from the constructor of IO::LockedFile.
Secondly - by changing the default behavior of the constructor of IO::LockedFile, we can get object of other class which means that we have a locked file that is locked with other scheme.
The default behavior of the constructor is determined by the global options.
We can access this global options, or the options per object using the method "set_option" and "get_option".
We can set the global options in the use line:
use IO::LockedFile 'Flock'; # set the default scheme to be Flock use IO::LockedFile ( scheme => Flock );
We can also set the options of a new object by passing the options to the constructor, as we will see below. We can change the options of an existing object by using the "set_option" method.
Which options are available?
There might be extra options that are used by one of the derived classes. So according to the scheme you choose to use, please look in the manual page of the class that implement that scheme.
Finally, some information that is connected to a certain scheme will be found in the classes that are derived from this class. For example, compatibility issues will be discussed in each derived classes.
The classes that currently implement the interface that IO::LockedFile defines are:
Rani Pinchuk, rani@cpan.org
Rob Napier, rnapier@employees.org
Copyright (c) 2001-2002 Ockham Technology N.V. & Rani Pinchuk. All rights reserved. This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2022-11-19 | perl v5.36.0 |