Gossamer Forum
Home : General : Perl Programming :

flock supported in ...

Quote Reply
flock supported in ...
hi

can you tell me how flock is supported in perl.

for example is it included in cgi.pm? or is it included in the perl interpreter installation 5++?
is it supported in the same manner under unix perl and windows activeperl?

i have been asked to write a portable script which should work on any platform (unix/windows) but i would like to know if i should instert flock with no problems. of course i use cgi.pm ;)

thank you.
Quote Reply
Re: [robyone] flock supported in ... In reply to
flock has a problem with locking files over a network if you should ever use it like that.. there is some code that immitates flock by using a lockfile, search for lockfile and you will find it.

chmod
Quote Reply
Re: [robyone] flock supported in ... In reply to
Yes flock is supported:

http://www.perldoc.com/.../pod/func/flock.html

flock will not work on windows although new versions of perl will allow for that however you should use something like:

flock (FH, 2) unless ($^O eq 'MSWin32');

...or set a global like:

$ISWIN = $^O eq 'MSWin32' ? 1 : 0;

...then:

flock (FH, 2) unless $ISWIN;
Quote Reply
Re: [Paul] flock supported in ... In reply to
I saw a neat trick the other day:

BEGIN {
if ($^O eq 'MSWin32') {
*CORE::flock = sub { 1 };
}
}

at the top of your script and then you don't need to add if's at the end of each flock call. Just call it normally.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] flock supported in ... In reply to
Cool. So simple too Smile
Quote Reply
Re: [Alex] flock supported in ... In reply to
You know something? That's genuis! Never even thought something like that. Simple, elegant, and effective.

Stealing thta. Thanks :)
Quote Reply
Re: [Alex] flock supported in ... In reply to
Hello Alex, I tried :

Code:


#!/perl/bin/perl

# This is perl, v5.6.0 built for MSWin32-x86-multi-thread
# On Win 98

BEGIN {
if ($^O eq 'MSWin32') {
*CORE::flock = sub { 1 };
}
}

open (FH,">test.txt") or die "BROKEN at : $!";
flock (FH,2); # This is line 13
print FH "Hello world\n";
close FH;
# Results in :
# flock() unimplemented on this platform at test.pl line 13.


Is this how it might be done ?

Thanks

kode
Quote Reply
Re: [robyone] flock supported in ... In reply to
Actually, flock doesn't work in Win 9.X, but it does work in WinNT and Win2K servers.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [kode] flock supported in ... In reply to
Hello all, I did a google search and found :

Code:


#!/perl/bin/perl



# This is perl, v5.6.0 built for MSWin32-x86-multi-thread
# On Win 98



BEGIN {
unless (eval 'flock(STDIN, 0),1') {
eval 'use subs "flock"; sub flock { }';
}
}



open (FH,">test.txt") or die "BROKEN by reason : $!";
flock (FH,2);
print FH "Hello world\n";
close FH;


Did work on Win 98 with out errors. I did get Hello world in test.txt. So the flock Win 98 error appears gone.

Does this work on Unix / Linux / NT 4 / NT 2000 / XP ?

I found this at : http://www.faqchest.com/...l00040515_21663.html

in a reply from Tom Phoenix part way down the page.

Thanks

kode

Last edited by:

kode: Oct 13, 2002, 5:05 PM
Quote Reply
Re: [kode] flock supported in ... In reply to
I don't know about XP, but the codes will work for all other OS you've mentioned.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [kode] flock supported in ... In reply to
Yes your original example should work.

Alex's code is "overriding" the core function and just returns true so if you are on a windows platform your flock() call will be doing nothing other than returning 1, it is only on other platforms that flock will actually be called properly.

Activestate's latest ports automatically handle flock if it is unimplemented but obviously not all hosts upgrade so you'd need that code for compatibility.

Last edited by:

Paul: Oct 14, 2002, 2:32 AM
Quote Reply
Re: [Alex] flock supported in ... In reply to
ok, but assuming one has to write a code for multiple platforms?

also, is there a way to automatically lock all of the files opened... with one command only. i.e. at the top of the script? this would be great :)

Last edited by:

robyone: Oct 16, 2002, 8:01 AM
Quote Reply
Re: [robyone] flock supported in ... In reply to
this is what i got .. please confirm

only activeperl could not support flock()

so I only need the following in the perl script to make it work on win servers not supporting flock function:

Code:

if ($^O eq 'MSWin32') {
*CORE::flock = sub { 1 };
}
Quote Reply
Re: [robyone] flock supported in ... In reply to
Yeah that's fine, remember the BEGIN {} block though.
Quote Reply
Re: [Paul] flock supported in ... In reply to
ok. last question.

is flock() necessary only if multiple users open for writing?

i guess it is not necessary if another user only read the file.

please confirm
Quote Reply
Re: [Paul] flock supported in ... In reply to
Activestate's latest ports automatically handle flock if it is unimplemented but obviously not all hosts upgrade so you'd need that code for compatibility.


do you mean that automatically locks itself withour requiring flock() command?

i was wondering if the script can use flock on win (when supported) when there is BEGIN { if ($^O eq 'MSWin32') { *CORE::flock = sub { 1 }; } }
Quote Reply
Re: [robyone] flock supported in ... In reply to
Quote:
do you mean that automatically locks itself withour requiring flock() command?

No what I mean is that normally on a windows platform if you call flock() then you will get an error along the lines of "flock unimplemented on this platform" but the latest ports of activeperl will prevent that error and I'm not sure of how it works around it but it probably implements something similar to the code Alex provided above.

Basically flock will not work on a windows platform, all these work arounds do is prevent you from having to add lots of checking to your code like:

flock ( ... ) unless ($^O eq 'MSWin32');

Last edited by:

Paul: Oct 16, 2002, 10:43 AM
Quote Reply
Re: [robyone] flock supported in ... In reply to
Yes you don't need to use file locking if your are only reading.
Quote Reply
Re: [kode] flock supported in ... In reply to
I tested the code :

BEGIN {
if ($^O eq 'MSWin32') {
*CORE::flock = sub { 1 };
}
}

on Perl for windows and it failed with flock() unimplemented on this platform

That is why I did a google search and found the second code that did work.

Alex's version does look simple but it did not work, while it did pass a perl -cw test

I am still wondering ? Did I do something wrong ? or is it broken ?

Thanks

kode


Quote Reply
Re: [kode] flock supported in ... In reply to
Hmm I get no errors using flock even with warnings on. I'm using Perl 5.6.1 Build 633