in reply to Filename from filehandle?

In general, you can't get a filename from a filehandle. What name would you use if STDIN were passed? :)

>Are there any other problems anyone sees?

Possibly the use of $lock_type. It looks like you're assuming that the flock() will fail if the file is already locked. That's true only on non-blocking filehandles or using LOCK_NB or'd with the second argument of flock(). Otherwise it's going to stop there and wait for the lock until it's available.

And beware of flock'ing filehandles that are already open...

Replies are listed 'Best First'.
Re: Re: Filename from filehandle?
by Xxaxx (Monk) on Apr 24, 2001 at 10:40 UTC
    Could you talk more about "And beware of flock'ing filehandles that are already open... "?

    I don't think I've ever applied a lock routine more than once. But now you've got my curiousity. What symptoms might occur if at some point I mess up and flock a file for the second time.

    Sign me curious
    Claude

      Here is the idea of what can happen:
      STEP 1: Wait for the first lock to be lost. STEP 2: Lose the first lock STEP 3: (Why don't we get here?)...
      This state of affairs is known as a "deadlock" and is usually considered Not Good.
      Filehandles that are already opened (especially for writing) have may have already been modified, before the lock was applied. Consider:
      open(FOO, ">/tmp/foo") || die; # You've altered the file! flock(FOO, LOCK_EX) || warn; # And now you bother to lock it?
      Yeah, it's silly. But you'd be amazed at how often it happens.