SPUG: Does -i invocation lock files? was Re: Does tie()...

Lou Hevly lou at visca.com
Fri Jun 16 03:32:01 CDT 2000


At 09:49 14/06/00 -0700, Tim Maher/CONSULTIX wrote:
>I'm teaching my "Intermediate Perl" class this week, and some questions
>about file-locking have arisen.  I could probably find the answers myself by
>reading some source code, but I'd rather spend my time helping my students
>on their lab assignments instead, so I thought I'd ask if anybody knows
>the answers:
>
>	1) Does the -i invocation option lock the files it processes?
>	(Seems like it should, but if it does, that's not documented!)

AFAIK, whether or not the -i invocation locks is irrelevant if you use 'semaphor' file locking; you might point your students to Mark-Jason Dominus's article at
http://www.plover.com/~mjd/perl/TPC/1998/Hardware-notes.html#Semaphore_Files

Here's a demo that deletes a username from .htpasswd; if this *isn't* foolproof, then I would very much appreciate hearing how it might fail. We'll assume that this is either the only program on the system writing to .htpasswd, or that all other programs that do use the same semaphor locking file.

use Fcntl qw(:flock);
@ARGV = '/path/to/.htpasswd';
my $semaphor = '/path/to/file.lock';
open S, "> $semaphor" or die $!;
flock S, LOCK_EX;
$^I = '.orig';
while (<>) {
  print unless /^$username/
}
close F;  close S;

-- 
All the best (Adéu-siau),
Lou Hevly
lou at visca.com
http://www.visca.com

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 For Subscriptions, Email to majordomo at pm.org:  ACTION  spug-list  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email address





More information about the spug-list mailing list