Pathologically Eclectic Rubbish Lister | |
PerlMonks |
There are three good modules you may wish to use:
my $data = "this is a string of text"; my @dataset = qw( this is an array of text ); my %datagroup = ( hash => "mine", text => "yours" );
Other useful methods include store() and retrieve(), which provide access to a named disk file.use Storable; my $stored = freeze [ \$data, \@dataset, \%datagroup ]; print $stored; # just to see what it looks like, in binary encoding my $thawed = thaw $stored; my ($newdata, $newdataset_ref, $newdatagroup_ref) = @$thawed; # cop +ies of original variables (*data, *dataset, *datagroup) = @$thawed; # restore into the origin +al variables
The biggest difference right there is that these methods work on a list, not an anonymous array.use FreezeThaw; my $stored = freeze (\$data, \@dataset, \%datagroup); print $stored; # even trickier encoding my @thawed = thaw $stored; my ($newdata, $newdataset_ref, $newdatagroup_ref) = @thawed; (*data, *dataset, *datagroup) = @thawed;
Even better, freeze() and thaw() called on a blessed object (what we really want to persist, right?) calls the Freeze or Thaw method, respectively, of that object. If none is provided, FreezeThaw has already installed UNIVERSAL::Freeze and UNIVERSAL::Thaw.
Note that the Dump subroutine takes two anonymous arrays. The first is a list of scalars (or references) to dump, and the second is a list of variables to which to assign the dumped data. (You can leave off the second list, but you'll end up with autogenerated names like $VAR1, $VAR2, and so on.)my $stored = Data::Dumper->Dump( [ $data, \@dataset, \%datagroup ], [ qw(data *dataset *datagroup )] ); print $stored; eval $stored;
Dumping an object results in all of the member data of the object being stored. There's something else magical that happens, though, as evidenced by this dump of an instance of Hello::Hi (from Jellybean):
When this is run through eval() (and assuming you have Hello::Hi available in memory somewhere already), you can call the correct methods. That is, $hi->main() and $hi->say_hi() produce the expected results as if the object had not been dumped and eval()'d.$hi = bless( { 'Data' => {}, 'Container' => 'Container', 'Info' => { 'modified' => '0', 'name' => 'Hi', 'author' => 'chromatic', 'date' => '1 March 2000', 'desc' => 'silly demonstration for Jellybean' }, 'main' => sub { "DUMMY" }, 'say_hi' => sub { "DUMMY" } }, 'Hello::Hi' );
for more information, see the documentation for Storable, FreezeThaw, and Data::Dumper, or Object Oriented Perl.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Object Serialization Basics
by kappa (Chaplain) on Apr 22, 2002 at 12:57 UTC | |
by Anonymous Monk on Jul 19, 2002 at 15:39 UTC | |
by siracusa (Friar) on Jul 21, 2005 at 17:01 UTC | |
Re: Object Serialization Basics
by belg4mit (Prior) on Apr 24, 2002 at 14:24 UTC | |
Re: Object Serialization Basics
by polettix (Vicar) on Mar 11, 2006 at 15:45 UTC | |
by chromatic (Archbishop) on Mar 11, 2006 at 20:09 UTC |