For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > September 2004 > Problem with flock









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem with flock
Rick Triplett

2004-09-27, 8:55 pm

Variations on the following code snippet have run successfully before,
but now the compiler halts with the complaint
" Bareword "LOCK_EX" not allowed while "strict subs" in use at
student_info_viewer.cgi line 47"
Can someone help me see what I am overlooking?

# Declare the 'working' datahash, then tie it to file
my %datahash;
my $db = tie %datahash, 'DB_File', $file_to_view, O_RDWR | O_CREAT, 0644
or die "Can't initialize database: $!\n";
my $fd = $db->fd(); # get a file descriptor
open DATAFILE, "+<&=$fd" or die "Can't safely open file: $!\n";
flock ( DATAFILE, LOCK_EX )
or die "Unable to acquire exclusive lock: $!. Aborting";
undef $db; # Avoid untie problems

Wiggins d Anconia

2004-09-27, 8:55 pm

> Variations on the following code snippet have run successfully before,
> but now the compiler halts with the complaint
> " Bareword "LOCK_EX" not allowed while "strict subs" in use at
> student_info_viewer.cgi line 47"
> Can someone help me see what I am overlooking?
>


You haven't shown us your 'use' statements, I suspect you didn't import
the LOCK_ constants,

perldoc -f flock

Adding:

use Fcntl ':flock'; # import LOCK_* constants

Should help. If it doesn't post the new errors,

http://danconia.org

> # Declare the 'working' datahash, then tie it to file
> my %datahash;
> my $db = tie %datahash, 'DB_File', $file_to_view, O_RDWR | O_CREAT, 0644
> or die "Can't initialize database: $!\n";
> my $fd = $db->fd(); # get a file descriptor
> open DATAFILE, "+<&=$fd" or die "Can't safely open file: $!\n";
> flock ( DATAFILE, LOCK_EX )
> or die "Unable to acquire exclusive lock: $!. Aborting";
> undef $db; # Avoid untie problems
>



Gunnar Hjalmarsson

2004-09-27, 8:55 pm

Wiggins d Anconia wrote:
> You haven't shown us your 'use' statements, I suspect you didn't import
> the LOCK_ constants,
>
> perldoc -f flock
>
> Adding:
>
> use Fcntl ':flock'; # import LOCK_* constants
>
> Should help.


I think you need to do:

use Fcntl qw(:DEFAULT :flock);

or else Perl will complain about O_RDWR and/or O_CREAT instead. The
latter are imported by default when using Fcntl, but need to be
explicitly imported if you explicitly import anything else.

perldoc Exporter

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com