I am trying to use L Stein's file locking subroutine from 'Official Guide
to Programming with CGI.pm' for a light duty cgi program I am writing. I'd
like to use 'use strict', but I can't get perl to agree with the return
at the end of the subroutine. (See return below)

Here is a sample program:

*******

#!/usr/local/bin/perl

#use strict;
use warnings;

my $file = 'data';
my $TIMEOUT = 10;
sub LOCK_SH { 1 };
sub LOCK_EX { 2 };
sub LOCK_UN { 8 };

sub filelock ($$) {
    my $path = shift;
    my $for_writing = shift;

    my ($lock_type,$path_name,$description);
    if ($for_writing) {
        $lock_type = LOCK_EX;
        $path_name = ">>$path";
        $description = 'writing';
    } else {
        $lock_type = LOCK_SH;
        $path_name = $path;
        $lock_type = LOCK_SH;
        $path_name = $path;
        $description = 'reading';
    }

    my ($msg,$oldsig);
    my $handler = sub { $msg='timed out'; $SIG{ALRM}=$oldsig; };
    ($oldsig,$SIG{ALRM}) = ($SIG{ALRM},$handler);
    alarm($TIMEOUT);

    open (FH,$path_name) or
       warn("Couldn't open $path for $description: $!"), return undef;

    # now try to lock it
    unless (flock (FH,$lock_type)) {
       warn("Couldn't get lock for $description (" . ($msg || "$!") . ")");
       alarm(0);
       close FH;
       return undef;
       close FH;
       return undef;
    }

    alarm(0);
    return FH;   # error from perl -cw is: Bareword "FH" not allowed while
                 # "strict subs" in use at sample line 47.
}

sub fileunlock {
    my $fh = shift;
    flock($fh,LOCK_UN);
    close $fh;
}

my $fh = &filelock($file,1);

print $fh "testing\n";

&fileunlock($fh);

*******

What do I need to change to allow 'use strict;' to bless this subroutine??
Or, is there a better way to prevent two writes from clobbering one
another?

Thanks,
Eric


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to