Hello all,
I am a relatively inexperienced Perl programmer (still working my way through Intermediate Perl).
While working on using DBM::Deep, I noticed some behavior I cannot explain.
I am using DBM::Deep 0.983 because there are no later versions available from ActiveState.
Here is my code:
#!/perl/bin/perl
use strict;
use warnings;
use DBM::Deep;
use Data::Dumper;
unlink("test.db");
my $database = DBM::Deep->new(
file => "test.db",
locking => 1,
autoflush => 1 );
# Test array that contains a hash reference as an element
my @array = ("Mac Address", "PCNAME", 1, 1187000400, { 'bob' => 23, '
+alice' => 20 });
print("\@array\n", Dumper(\@array)); # Shows the contents of the ar
+ray, with no blessed references and nothing tied to the DBM::Deep dat
+abase
$database->{141} = \@array; # Assigns the array reference as a valu
+e of the DBM::Deep database hash
print("\@array\n", Dumper(\@array)); # Now the original array's int
+ernal hash is tied or blessed or something to the database
my $temp = $database->{141}->export; # Export should return a refer
+ence to an array in which the internal hash is not tied to the databa
+se anymore
print("\$temp\n", Dumper($temp)); # Dumper shows no tied or blessed
+ hash; seems like it worked.
my @new_array = @{ $temp }; # Dereferencing the array reference to
+reconstitute the array.
print("\@new_array\n", Dumper(\@new_array)); # The new array's hash
+ reference is normal.
@array = @{ $temp }; # Assigning the array reference to the origina
+l array variable.. should work fine.
print("\@array\n", Dumper(\@array)); # This prints a hash reference
+ that is still tied to the database. WTF?
my @array = @{ $temp }; # Assigning the array reference to the orig
+inal array variable but redeclaring it using my.
print("\@array\n", Dumper(\@array)); # This works... Why is redecl
+aring the variable important? Why doesn't the reassignment work?
My questions, as shown in the comments, are:
1) Why does the hash reference, which prints clearly on its own, show a connection to the database when dereferenced and stored in the original array?
2) Why does using a new array, or redeclaring the array using 'my' fix this? I assume the answer is the same for both new and 'my' but do not understand either way.
Note: Running this on my Linux machine instead of my Windows machine does not yield a fix using 'my' again, but instead gives a warning of '"my" variable @array masks earlier declaration in same scope at test_dbm.pl line 30'. Of course, my Linux machine has DBM::Deep 1.0007 installed.
Any enlightenment you can provide would be most appreciated.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.