Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Flocking

Posted by TedH (TedH), 21 April 2008
Hi guys:
Just a question really. In the code below (not mine - I'm learning about handling flock differently), I notice the use of a semi-colon for a write, but not in the other cases.

The code works fine, just wondering why the difference?

Code:
# Variable
$flock = "0";  # Set to to 1 for Unix

### No semi-colon for reading and on >> appends.
if ($flock) {flock(DATABASE, 2)}
etc-etc.....

### Why the semi-colon on this one? Is it becuz of the > type write?
open (DATABASE, ">".$file);
if ($flock) {flock(DATABASE, 2);}
etc-etc.....


Cheers - Ted


Posted by KevinAD (KevinAD), 21 April 2008
The semi-colon has nothing to do with the use of flock or anything else for that matter.

The last line (or in this case the only line) of a block of code: {}, does not need a semi-colon, it's optional. In my opinion, for consitancy sake you should always put one.  

Posted by KevinAD (KevinAD), 21 April 2008
Hard core perl coders will tell you to use the Fcntl module and import the flock constants into your program. I am a bit ambivalent on this issue but I tend to use Fcntl too and import the symbolic names, in some cases it might be necessary although I am not sure about that, maybe Graham knows. See the flock() function and the Fcntl module for more details.

Posted by TedH (TedH), 21 April 2008
Hi Kevin, Thanks. I usually just do a
Code:
# flock(DFILE, 2);

Then remove the comment when I put it on my live Unix server. My local testbed is Indigo perl on a Windows PC, so it's commented out there. Which is okay if there's just one, But - when there are hundreds or thousands of lines of code, with a bunch of flocks in, I figured I need a more efficient way to do it - hence the variable option.

I'll use the semi-colon as suggested.

Cheers - Ted

Posted by KevinAD (KevinAD), 22 April 2008
It was evident what you were doing by this line:

# Variable
$flock = "0";  # Set to to 1 for Unix

Been there, done that, too. You can make that dynamic:

my $flock = ($^O =~ /MSwin32/i) ? 1 : 0;

Then you don't have to comment it out and it will work on Windows and Unix/Linux.



Posted by TedH (TedH), 22 April 2008
That's neat  Thanks Kevin.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho