Every so often I put out a small newsletter containing Tips and Tricks, as well as web URL's that might be interesting or helpful, and perhaps other news that may deal with Perl, Linux, or any of the other topics I teach. If you are interested in this, please sign up for my newsletter, and feel free to unsubscribe at any time.
David Slimp's Newsletter
for Fri May 3 19:51:48 CDT 2002
##### Program of the week
A few weeks ago I had a student asking about how to do file locking
in Perl. For the task she was doing I suggested dynamically named
temporary files rather than file locking, but here are two examples of
how you could use perl's "flock" command to handle file locking.
I would REALLY recommend reading the documentation on flock before
implementing it in an important project on your system as there can
be many quirks with it:
perldoc -f flock
Many systems might not support file locking, and this first example/test
program tests to see what your system supports.
===============================================================================
#!/usr/bin/perl -w
# flock_test.pl - example and test of file locking
#
use IO::File; # needed for OO methods to file handling
use Fcntl qw(:flock); # provides the "flock" command
# new_tmpfile is an object constructor for IO::File
*FH1 = new_tmpfile IO::File or die "$!" ;
# test locking and trap any errors
eval { flock(FH1, LOCK_SH) };
$@ and die "system doesn't support flock: $@\n";
# test file handle duplicating
open(FH2, ">>&FH1") or die "cant dup FH: $!\n";
# test for shared and exclusive locks
if ( flock(FH2,LOCK_SH | LOCK_NB) ) {
print "system supports shared locks\n";
} else {
print "system only supports exclusive locks\n";
}
===============================================================================
In this test you can see file locking in action!
Follow the directions in the DESCRIPTION and enjoy...
===============================================================================
#!/usr/bin/perl -w
# flockExample.pl: show example of locking and writing
#
# DESCRIPTION:
# open two terminal windows side by side, start this program once in
# the first window, then also run it immediately in the second window.
# The second run will wait to write until the first has released the lock.
#
# created by David Slimp <rock808@DavidSlimp.com>
# - Fri Apr 19 20:45:45 CDT 2002
use Fcntl; # provides the 'flock' function
print "Hello. I am process: $$\n";
open(OUT, ">>flockExample.data") or die "Can't open database: $!";
# call locking subroutine (note milliseconds between open and lock)
&lock;
# we take a few seconds to write to the file while another instance of this
# program tries to also get a lock and write to the file
for (1..8){
print OUT "process $$ is adding this line # $_\n";
sleep 2;
}
&unlock;
sub lock {
print "Now trying to get lock...\n";
flock(OUT,2) or die "could not lock file: $!"; # exclusive lock with "2"
print "locked!\n";
# move to end of file in case someone else appended while we were waiting
seek(OUT, 0, 2);
}
sub unlock {
flock(OUT,8); # unlock with "8"
print "UN-locked!\n";
}
===============================================================================
##### URLs
During the same week, there was talk about a need for date manipulation,
which I have actually heard from several students over the years I've been
teaching. There are currently several ways to do this kind of thing
(such as find the difference between two dates, what date will it be in
some number of days, etc), but here is a good perl module than can make
your life much easier!
Class::Date 1.0.9
Class::Date provides a date data-type for Perl. You can create new
Class::Date objects with a constructor from different scalar formats, array
refs, and hash refs, and then you can easily manipulate it by the built-in
"+" and "-" operators (e.g., $date=date([2001,03,15])+'3Y 1s'). Relative
date types also available.
project page: http://freshmeat.net/projects/class_date/ date: Monday, February 25th 2002 18:21
license: Artistic License
project id: class_date
I was notified about the new release of that module automatically by another
piece of software written in perl called "whatsnewfm.pl", which parses the
daily newsletter from http://www.freshmeat.netand notifies the user via email
of new releases of software they have marked as "interesting".
If you are not familiar with freshmeat.net then I highly recommend taking
a look there for all kinds of cool (mostly free) software for Linux.
And if you like what you see there at Freshmeat, then I would also suggest
grabbing and installing a copy of "whatsnewfm.pl" so that you can keep up
with all the latest releases much more automatically.
##### Thanks
Thanks for subscribing and showing an interest in getting more out of these
topics! I enjoy the interaction with my students and like to see them
continuing to move forward... especially after the class.
However, if you are interested in un-subscribing, please go to:
http://www.DavidSlimp.com/newsletter
If you are interested in taking a class then please look at my schedule
of upcoming classes at the link below. If you are interested in a class
that is not listed, then please contact me and we can set one up for you!
http://www.DavidSlimp.com/schedule.html
More next week!
David Slimp
david@DavidSlimp.com
http://www.DavidSlimp.com