Gift Wrapping, part II: Locking the Room
Continuing on the topic of gift wrapping, another traditional manoeuver to wrap gifts in peace consist on locking yourself in a room (typically, with a sign reading Do Not Enter on the door) as you perform the deed.
With programs, you'll want to do the same thing if your program should only have one instance running at any given time. You want to have a lock file, but then you have to see how arcane things like
flock works, think about cross-platform issues… or, maybe, you could use File::Flock::Tiny:
Niftier still, it turns out that the above example is overkill, because
File::Flock::Tiny will automatically release the lock when its
$lock object goes out of scope. Knowing that, the
$lock->release line is not necessary. This auto-release trick plays also very nicely with Moose. Want to have a script with lockfile functionality? Here goes:
With that, your script will judicously lock itself in the bedroom before taking out the presents from their secret place. As soon as the object is done and get out of scope (most likely as the the script terminates), the lock will be automatically removed.
Oh. This being said, check under the bed. Chances are that locking the room, no matter how cleverly, won't help you if your tikes are already hidding under the bed. Just saying…