in reply to Does Semaphore Work?
in thread Safe Counter

Are you use-ing strict, warnings, and Fcntl ':flock' ? If you call flock while LOCK_EX is seen as undef or zero (in numeric context), you won't get the effect you want, and the semaphore will indeed fail silently.

$ perl -e'print +LOCK_EX, $/' LOCK_EX $ perl -e'print 0+LOCK_EX, $/ 0 $ perl -Mwarnings -e'print 0+LOCK_EX, $/' Argument "LOCK_EX" isn't numeric in addition (+) at -e line 1. 0 $ perl -Mstrict -Mwarnings -e'print 0+LOCK_EX, $/' Bareword "LOCK_EX" not allowed while "strict subs" in use at -e line 1 +. Execution of -e aborted due to compilation errors. $ perl -Mstrict -Mwarnings -MFcntl=:flock -e'print 0+LOCK_EX, $/' 2 $

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Does Semaphore Work?
by theorbtwo (Prior) on May 07, 2004 at 05:27 UTC

    To expand a little on what Zaxo said...

    Perl generaly tries to be a very helpful language. By default, it lets you write some strings without quoting them, simply by saying something like print string;. Unfornatly, there's no way to tell this apart from something that you meant to be a function, but never defined, and called without parenthesies. That is, when you write print foo;, you may have meant print "foo";, or you may have meant print foo();. By default, perl thinks you meant the second if a subroutine named foo actualy exists, and the first if there is no such subroutine.

    However, this can often create difficult-to-debug problems, and isn't really that helpful in the first place (typing the quotes isn't that hard, and makes your code easier to read). Thus, putting a use strict; at the beginning of your code disables this feature -- print foo; means to run the sub named foo, and print the result. If there is no sub named foo defined when the code is run, then it's an error.

    (use strict also changes other things that make it more difficult to cause yourself problems -- it makes you declare your variables, and it keeps you from talking about variables by name when what you really meant was to refer to them -- don't worry about that last. use warnings, on the other hand, warns you when what you said is most likely not what you meant.

    Using both of them, as a matter of habit, will make it more obvious when you're messing up, and how, so you can help yourself.