in reply to TIMTOWTDI - a counter

I'd import the :DEFAULT and :flock constants from Fcntl, use sysopen with O_RDWR|O_CREAT flags, use flock with the LOCK_EX flag, test the value of the read operation or supply 0, and add tests on the flock, seek, and truncate calls as well.

Replies are listed 'Best First'.
Re: Re: TIMTOWTDI - a counter
by Juerd (Abbot) on May 26, 2002 at 16:43 UTC

    I'd

    Care to translate it into code? :)

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

      Sorry. I meant those suggestions to be pretty much straighforward modifications or additions to your code, not anything entirely different.
      use Fcntl qw/:DEFAULT :flock/; sub counter { my $file = shift; sysopen(COUNTER, $file, O_RDWR|O_CREAT) || warn $! && return; flock(COUNTER,LOCK_EX) || warn $! && return; my $count = <COUNTER> || 0; seek(COUNTER,0,0) || warn $! && return; truncate(COUNTER,0) || warn $! && return; print(COUNTER $count + 1, "\n") || warn $! && return; close(COUNTER) || warn $! && return; return $count + 1; }