Qiang has asked for the wisdom of the Perl Monks concerning the following question:

I got this simple script that i can't get it work. I am trying to tie an existing db but it dies when tie.
the db file exists in the same dir where the perl script is. and it's read/writable by me.

#!/usr/bin/perl -w use strict; use DB_File; my %db; tie (%db,'DB_File',"mydb.db",O_RDONLY) or die "tie : $!"; untie %db;
it simply dies with tie :  at t.pl line 9.

I followed tye's suggestion and traced it with perl debugger. but I don't see anything wrong (still new to perl debugger) here is the trace if it helps

main::(t.pl:9): tie (%db,'DB_File',"movies.db",O_RDONLY) or die "tie : + $!"; DB<1> s Fcntl::AUTOLOAD(/usr/lib/perl5/5.6.0/i386-linux/Fcntl.pm:203): 203: (my $constname = $AUTOLOAD) =~ s/.*:://; DB<1> s Fcntl::AUTOLOAD(/usr/lib/perl5/5.6.0/i386-linux/Fcntl.pm:204): 204: my $val = constant($constname, 0); DB<1> s Fcntl::AUTOLOAD(/usr/lib/perl5/5.6.0/i386-linux/Fcntl.pm:205): 205: if ($! != 0) { DB<1> s Fcntl::AUTOLOAD(/usr/lib/perl5/5.6.0/i386-linux/Fcntl.pm:216): 216: *$AUTOLOAD = sub { $val }; DB<1> s Fcntl::AUTOLOAD(/usr/lib/perl5/5.6.0/i386-linux/Fcntl.pm:217): 217: goto &$AUTOLOAD; DB<1> s Fcntl::__ANON__[/usr/lib/perl5/5.6.0/i386-linux/Fcntl.pm:216](/usr/lib +/perl5/5.6.0/i386-linux/Fcntl.pm:216): 216: *$AUTOLOAD = sub { $val }; DB<1> s DB_File::TIEHASH(/usr/lib/perl5/5.6.0/i386-linux/DB_File.pm:264): 264: tie_hash_or_array(@_) ; DB<1> s DB_File::tie_hash_or_array(/usr/lib/perl5/5.6.0/i386-linux/DB_File.pm: +244): 244: my (@arg) = @_ ; DB<1> s DB_File::tie_hash_or_array(/usr/lib/perl5/5.6.0/i386-linux/DB_File.pm: +246): 246: my $tieHASH = ( (caller(1))[3] =~ /TIEHASH/ ) ; DB<1> s DB_File::tie_hash_or_array(/usr/lib/perl5/5.6.0/i386-linux/DB_File.pm: +249): 249: if @arg >= 5 && ref $arg[4] && $arg[4] =~ /=HASH/ && t +ied %{ $arg[4] } ; DB<1> s DB_File::tie_hash_or_array(/usr/lib/perl5/5.6.0/i386-linux/DB_File.pm: +252): 252: if ($db_version > 1 and defined $arg[4] and $arg[4] =~ /RE +CNO/ and 253: $arg[1] and ! -e $arg[1]) { DB<1> s DB_File::tie_hash_or_array(/usr/lib/perl5/5.6.0/i386-linux/DB_File.pm: +259): 259: DoTie_($tieHASH, @arg) ; DB<1> s tie : at t.pl line 9, <IN> line 14. Debugged program terminated. Use q to quit or R to restart,
line 244:        my (@arg) = @_ ; here @_ contains DB_File,mydb.db,0

Replies are listed 'Best First'.
Re: DB_File tie failed
by jZed (Prior) on Jun 03, 2004 at 16:53 UTC
    Try:
    eval { tie %h, $tie_class, @tie_args }; die "Cannot tie($tie_class @tie_args): $@" if $@;
Re: DB_File tie failed
by eserte (Deacon) on Jun 03, 2004 at 19:05 UTC
    Maybe the database is corrupted? Sometimes it helps to dump and reload the database file with db_dump and db_load. It might be a problem of the berkeley db library itself --- recently I was surprised that I cannot tie RECNO databases in O_RDONLY mode (berkeley db 4.0 on RedHat 8.0). In that case there was also no error message in $!.
      I have updated the DB_File and Perl to the latest. but still no luck. I have no problem working on another db file which generated a while ago using the same script.

      finally, i came to realize that the db I am having problem were generated on a windows machine using activeperl. once I regenerate the db file under the system i am working on(linux) , it is okay now.

      thanks again for your help.

Re: DB_File tie failed
by borisz (Canon) on Jun 03, 2004 at 16:56 UTC
    Hi, Perhaps your file did ot exists? perl 5.6.0 is pretty old too. Try it with tie (%db,'DB_File',"mydb.db") or die "tie : $!"; It creates the file if it is not there.
    Sure I admit there should be a better errormessage, but I get a good one with perl 5.8.3.
    Boris