Open an existing DB_Ffile file failed

P

panxd01

Hi, I tried to open an existing DB_File type of file to read the
records. However, it kept getting error saying "cannot open file: File
exists". Did some research on web but couldn't find any useful info.
Could someone please explain why? Below is a sample of code which I
got from the perl.com:

use strict ;
use DB_File ;
use vars qw( %h $k $v ) ;

tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0640, $DB_HASH
or die "Cannot open file 'fruit': $!\n";

# Add a few key/value pairs to the file
$h{"apple"} = "red" ;
$h{"orange"} = "orange" ;
$h{"banana"} = "yellow" ;
$h{"tomato"} = "red" ;

# Check for existence of a key
print "Banana Exists\n\n" if $h{"banana"} ;

# Delete a key/value pair.
delete $h{"apple"} ;

# print the contents of the file
while (($k, $v) = each %h)
{ print "$k -> $v\n" }

untie %h ;

When I second time run the code, I got into the error because 'fruit'
was created before. My perl version is 5.8.8 if this makes difference.
Thanks a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top