DB_File
view release on metacpan or search on metacpan
Cannot tie second time: Invalid argument at bad.file line 14.
Although the error message above refers to the second tie() statement
in the script, the source of the problem is really with the untie()
statement that precedes it.
Having read L<perltie> you will probably have already guessed that the
error is caused by the extra copy of the tied object stored in C<$X>.
If you haven't, then the problem boils down to the fact that the
B<DB_File> destructor, DESTROY, will not be called until I<all>
references to the tied object are destroyed. Both the tied variable,
C<%x>, and C<$X> above hold a reference to the object. The call to
untie() will destroy the first, but C<$X> still holds a valid
reference, so the destructor will not get called and the database file
F<tst.fil> will remain open. The fact that Berkeley DB then reports the
attempt to open a database that is already open via the catch-all
"Invalid argument" doesn't help.
If you run the script with the C<-w> flag the error message becomes:
untie attempted while 1 inner references still exist at bad.file line 12.
Cannot tie second time: Invalid argument at bad.file line 14.
which pinpoints the real problem. Finally the script can now be
modified to fix the original problem by destroying the API object
before the untie:
...
$x{123} = 456 ;
undef $X ;
untie %x ;
$X = tie %x, 'DB_File', 'tst.fil' , O_RDWR|O_CREAT
...
=head1 COMMON QUESTIONS
=head2 Why is there Perl source in my database?
If you look at the contents of a database file created by DB_File,
there can sometimes be part of a Perl script included in it.
This happens because Berkeley DB uses dynamic memory to allocate
buffers which will subsequently be written to the database file. Being
dynamic, the memory could have been used for anything before DB
malloced it. As Berkeley DB doesn't clear the memory once it has been
allocated, the unused portions will contain random junk. In the case
where a Perl script gets written to the database, the random junk will
correspond to an area of dynamic memory that happened to be used during
the compilation of the script.
Unless you don't like the possibility of there being part of your Perl
scripts embedded in a database file, this is nothing to worry about.
=head2 How do I store complex data structures with DB_File?
Although B<DB_File> cannot do this directly, there is a module which
can layer transparently over B<DB_File> to accomplish this feat.
Check out the MLDBM module, available on CPAN in the directory
F<modules/by-module/MLDBM>.
=head2 What does "wide character in subroutine entry" mean?
You will usually get this message if you are working with UTF-8 data and
want to read/write it from/to a Berkeley DB database file.
The easist way to deal with this issue is to use the pre-defined "utf8"
B<DBM_Filter> (see L<DBM_Filter>) that was designed to deal with this
situation.
The example below shows what you need if I<both> the key and value are
expected to be in UTF-8.
use DB_File;
use DBM_Filter;
my $db = tie %h, 'DB_File', '/tmp/try.db', O_CREAT|O_RDWR, 0666, $DB_BTREE;
$db->Filter_Key_Push('utf8');
$db->Filter_Value_Push('utf8');
my $key = "\N{LATIN SMALL LETTER A WITH ACUTE}";
my $value = "\N{LATIN SMALL LETTER E WITH ACUTE}";
$h{ $key } = $value;
=head2 What does "Invalid Argument" mean?
You will get this error message when one of the parameters in the
C<tie> call is wrong. Unfortunately there are quite a few parameters to
get wrong, so it can be difficult to figure out which one it is.
Here are a couple of possibilities:
=over 5
=item 1.
Attempting to reopen a database without closing it.
=item 2.
Using the O_WRONLY flag.
=back
=head2 What does "Bareword 'DB_File' not allowed" mean?
You will encounter this particular error message when you have the
C<strict 'subs'> pragma (or the full strict pragma) in your script.
Consider this script:
use warnings ;
use strict ;
use DB_File ;
my %x ;
tie %x, DB_File, "filename" ;
Running it produces the error in question:
( run in 0.557 second using v1.01-cache-2.11-cpan-e1769b4cff6 )