Hack 44. Serialize Objects (Mostly) for Free


Store object data without mess, confusion, or big blobs of binary data.

Some programs really need persistent data, and sometimes mapping between objects and multiple tables in a fully-relational database is just too much work. This is especially true in cases where being able to edit data quickly and easily is importantthere's no interface more comfortable than your favorite text editor [Hack #12].

Instead of hard-coding configuration in a program, wasting your precious youth creating the perfect database schema, or doing XML sit-ups, why not serialize your important object data to YAML?

The Hack

If you use hash-based objects, it's very easy to serialize the datajust make a copy of the hash and serialize it:

use YAML 'DumpFile'; sub serialize {     my ($object, $file) = @_;     my %data            = %$object;     DumpFile( $file, \\%data ); }

This assumes, of course, that $object is the object you want to serialize and $file is the path and file to which to save the object.

If you use inside out objects [Hack #43], you have a bit more work to do:

package Graphics::Drawable; {     use Class::Std;     my %coords_of     :ATTR( :get<coords>   :init_arg<coords>   );     my %velocities_of :ATTR( :get<velocity> :init_arg<velocity> );     my %shapes_of     :ATTR( :get<shape>    :init_arg<shape>    );     sub get_serializable_data     {         my $self  = shift;         my %data;         for my $attribute (qw( coords velocity shape ))         {             my $method = 'get_' . $attribute;             $data{ $attribute } = $self->$method( );         }         return \\%data;     } }

Now your serialize( ) function can avoid breaking encapsulation and call get_serializable_data( ) instead. An object at the origin (coordinates of (0, 0, 0)) with a velocity of one unit per time unit along the X axis ((1, 0, 0)) and a Circle shape serializes to:

--- coords:   - 0   - 0   - 0 shape: Circle velocity:   - 1   - 0   - 0

If you want to make more objects, copy the file to a new location and modify it. Just be careful to keep the code valid YAML.[2]

[2] A task much easier than writing valid XML by hand...

Restoring objects is easy; just use YAML's LoadFile( ) method:

use YAML 'LoadFile'; sub deserialize {     my ($class, $file) = @_;     my $data           = LoadFile( $file );     return $class->new( $data ); }

Assuming your class constructor takes a hash reference keyed on attribute names (as Class::Std does), you're all set. Of course, this all presumes some sort of object factory that can manage instances, map files and paths to classes, and store and retrieve objects, let alone handle errors. Class::StorageFactory on the CPAN handles this.

If you have all of thisand only need data from an object's public interface (both constructor attributes and data accessible through accessors) to recreate the objectserializing to YAML or another simple plain-text format (JSON?) is fast, easy, flexible, and almost free.



Perl Hacks
Perl Hacks: Tips & Tools for Programming, Debugging, and Surviving
ISBN: 0596526741
EAN: 2147483647
Year: 2004
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net