Next | Lightweight Databases | 91 |
We could use a serialization module like Storable
It will convert arbitrary values to strings, and back:
use Storable; $hash{numbers} = freeze [1, 4, 2, 8, 5, 7];
$aref = thaw $hash{numbers}; print "@$aref\n";
1 4 2 8 5 7
This is kind of a pain
DB_File will do it automatically:
my $db = tied %hash; $db->filter_store_value(sub { $_ = Storable::freeze($_) }); $db->filter_fetch_value(sub { $_ = Storable::thaw($_) });
Now this works:
$hash{numbers} = [1, 4, 2, 8, 5, 7]; $aref = $hash{numbers}; print "@$aref\n";
freeze and thaw are called automatically
Note that the filters use $_ for input and output
Similarly, filter_store_key and filter_fetch_key
Next | Copyright © 2003 M. J. Dominus |