Lock files using the flock call. If the file to be locked does not exist, then the file is created. If the file was created then it will be removed when it is unlocked assuming it's still an empty file. Locks can be created by new'ing a File::Flock object.
Return the name of the temporary file associated with this object if the object was created using the "new" constructor.
Return the name of the temporary directory associated with this object if the object was created using the "newdir" constructor. Control whether the file is unlinked when the object goes out of scope. When the object goes out of scope, the destructor is called. If the object has been passed to a child process during a fork, the file will be deleted when the object goes out of scope in the parent.
Note that if a temp directory is your current directory, it cannot be removed - a warning will be given in this case. This is the basic function to generate temporary files. The behaviour of the file can be changed using various options:. Create a temporary file in the directory specified for temporary files, as specified by the tmpdir function in File::Spec.
Create a temporary file in the current directory using the supplied template. Useful for ensuring that a temporary filename has a particular extension when needed by other applications. Default is for the file to be removed if a file handle is requested and to be kept if the filename is requested. If the template is not specified, a template is always automatically generated. This temporary file is placed in tmpdir File::Spec unless a directory is specified explicitly with the DIR option.
If called in scalar context, only the filehandle is returned and the file will automatically be deleted when closed on operating systems that support this see the description of tmpfile elsewhere in this document. This is the preferred mode of operation, as if you only have a filehandle, you can never create a race condition by fumbling with the filename. This will return the filename based on the template but will not open this file.
Default is to always open the file to protect from possible race conditions. A warning is issued if warnings are turned on. Consider using the tmpnam and mktemp functions described elsewhere in this document if opening the file is not required.
This is supported only by some operating systems most notably BSD derived systems. By default, the temp file is created with file permissions. This is the recommended interface for creation of temporary directories. By default the directory will not be removed on exit that is, it won't be temporary; this behaviour can not be changed because of issues with backwards compatibility.
To enable removal either use the CLEANUP option which will trigger removal on program exit, or consider using the "newdir" method in the object interface which will allow the directory to be cleaned up when the object goes out of scope. Create a directory from the supplied template. This template is similar to that described for tempfile. Specifies the directory to use for the temporary directory. The temporary directory name is derived from an internal template. Prepend the supplied directory name to the template.
The template should not include parent directory specifications itself. Any parent directory specifications are removed from the template before prepending the supplied directory. Using the supplied template, create the temporary directory in a standard location for temporary files. Equivalent to doing. Parent directory specifications are stripped from the template itself. Create a temporary directory using the supplied template, but attempt to remove it and all files inside it when the program exits.
Note that an attempt will be made to remove all files from the directory even if they were not created by this module otherwise why ask to clean it up? Produces a fatal error if used on a machine that doesn't implement flock 2 , fcntl 2 locking, or lockf 3.
Two potentially non-obvious but traditional flock semantics are that it waits indefinitely until the lock is granted, and that its locks are merely advisory. Such discretionary locks are more flexible, but offer fewer guarantees.
This means that programs that do not also use flock may modify files locked with flock. Note the file path is quoted in the error message. This helps debug situations where you have a stray space at the start or end of the path. Do NOT use flock to unlock the file if you wrote to the file in the "do something" section above. This could create a race condition. The close call below will unlock the file for you, but only after writing any buffered data. Improve this answer.
John Siracusa Chris Bunch Chris Bunch Alex Dupuy Alex Dupuy 5, 2 2 gold badges 36 36 silver badges 34 34 bronze badges. Thanks for the fine explanation of the some of the distinctions between the underlying system calls. Gary Richardson Gary Richardson Object oriented Perl eek. The CPAN module looks good to me. I'd rather reuse code than spell it out myself every time, even if it's only a couple of lines.
Perl OO syntax is fairly hideous, but it works okay. Ryan P wrote: In this case the file is actually unlocked for a short period of time while the file is reopened. Aristotle Pagaltzis Aristotle Pagaltzis k 22 22 gold badges 95 95 silver badges 96 96 bronze badges.
Does seeking back to the beginning actually overwrite the contents of the file? That was my specific issue. I am testing with the die I was just trying to keep the code simple in the example.
I nevere thought of the exploit of the 2 arg open thanks I will keep that in mind. Yes, it will overwrite the contents. You should check close anyway. You can't do much, but you can at least tell the user and bail out, instead of silently continuing to run as if nothing had happened.
Sure, most of the times it will not, but when it does, you'd be in big trouble. Depending on perl flock implementation see flock 2 , lockf 3 or fcntl 2 — Matija Nalis. In my past experience, I have found it very easy to use and sturdy. This looks a bit more complicated than the flock structure.
0コメント