Документация
HTML CSS PHP PERL другое

Q&A

 
Previous Table of Contents Next

Q&A

Q1:

Can I store the structures from Hour 13, "References and Structures," in a DBM file or a text file?

A1:

The short answer is no, not easily. The longer answer is yes, but first you need to convert the "structure" over to a string that represents the data and the structure that contains it, and you then need to use that as a value in the DBM-tied hash. The module to do so is Data::Dumper.

Q2:

How do I lock DBM files?

A2:

DBM files can be locked using the semaphore locking system shown previously. Simply use the get_lock() and release_lock() functions from Listing 15.3 and put these around the DBM open and close functions:


get_lock();

dbmopen(%hash, "foo", 0644) || die "dmbopen: $!";

$hash{newkey}="Value";

dbmclose(%hash);

release_lock();


Q3:

Can I somehow just check to see whether flock is going to pause without actually having it stop?

A3:

Yes you can. A value that can be passed to flock causes it not to pause. (It is called a nonblocking flock.) To find out whether a flock would pause, put a |LOCK_NB after the lock type like this:


use Fcntl qw(:flock);

# Attempt to get an exclusive lock, but don't wait for it.

if (not flock( LF, LOCK_EX|LOCK_NB )) {

    print "Could not get the lock: $!";

}


You can even wait for a lock for a while, and then print a message if you don't get it eventually:


use Fcntl qw(:flock);

$lock_attempts = 3; 

while (not flock( LF, LOCK_EX|LOCK_NB )) {

    sleep 5;  # Wait 5 seconds

    $lock_attempts--;  # Count down chances...

    die "Could not get lock!" if (not $attempts);

}


    Previous Table of Contents Next
    © 2000- NIV