Open file for update
#!/usr/bin/perl
use warnings;
use strict;
use Fcntl qw(:seek :flock);
open LOGFILE, ">>", "/tmp/mylog" or die "Unable to open: $! \n";
# lock file for exclusive access
flock LOGFILE, LOCK_EX;
# now seek to end of file explicitly, in case it changed since the open
seek LOGFILE, 0, SEEK_END;
# write our log message
print LOGFILE "Log message...\n";
# remove lock and close file
flock LOGFILE, LOCK_UN;
close LOGFILE;
Related examples in the same category