in reply to Re^3: Proper way of passing around tie'd variable
in thread Proper way of passing around tie'd variable

I see what you mean. All that code that I am using came directly from the book CGI Programming with Perl , Chapter 10, Section 2 ("DBM Files"). I am not very experienced with file locking, and I wasn't really interested in the syncronization stuff when we covered it in my OS class (semaphores still send shivers up my spine). I figured I could just copy and paste the code from the book to get it to work, but I guess I was wrong.

So how do you suggest I deal with file locking? As explained in the book, the point of the $db variable is to get the file descriptor. Once that is done, we 'undef' it to decrement the reference count. That way, when hash is untie'd, the changes made to hash are committed to the file. But I guess instead of doing all that, I can use a different file as a lock?

I still find it peculiar that things work when there is a Dumper call in hash_init that reads the values. Can anybody explain why that happens?

Also, can you tell me what you had to change in the code that I posted? Because that really is what I am using..

  • Comment on Re^4: Proper way of passing around tie'd variable

Replies are listed 'Best First'.
Re^5: Proper way of passing around tie'd variable
by kyle (Abbot) on Jan 07, 2008 at 18:20 UTC

    Also, can you tell me what you had to change in the code that I posted?

    Most if it was just getting getting the use lines right. This line:

    flock DBM, LOCK_EX;

    ...originally had LOCK_EX in quotes. Since I always use warnings, it said unto me:

    Argument "LOCK_EX" isn't numeric in flock ...

    So how do you suggest I deal with file locking?

    Use a separate lock file, and lock it before you tie. I'd be really surprised if there isn't a module to do it for you, but I don't know off the top of my head what it would be.