Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Can I serialize an object and have it remember it's type?

by Anonymous Monk
on Jul 21, 2005 at 14:48 UTC ( #476859=note: print w/ replies, xml ) Need Help??


in reply to Re: Can I serialize an object and have it remember it's type?
in thread Can I serialize an object and have it remember it's type?

Note that "serializing" objects using Data::Dumper or YAML only work because they break encapsulation, and assume something about their implementation - for instance, that objects are implemented using a reference to a hash, and that the entire state of the object depends only on the content of said hash (or reference to an array, or scalar).

Serializing fly weigth objects or inside out objects will not work using Data::Dumper or YAML. You'll need to write your serialize/deserialize routines in (all) your classes.


Comment on Re^2: Can I serialize an object and have it remember it's type?
Re^3: Can I serialize an object and have it remember it's type?
by halley (Prior) on Jul 21, 2005 at 18:38 UTC
    Minor correction: object state serialization works for any blessed type in Perl, not just references to hashes.
    package XX; sub new { bless([1,2,3],__PACKAGE__) } package YY; sub new { $abc="abc"; bless(\$abc,__PACKAGE__) } package ZZ; sub new { $ott=123; bless(\$ott,__PACKAGE__) } package main; use Data::Dumper; print +(Dumper(new XX)); print +(Dumper(new YY)); print +(Dumper(new ZZ)); __OUTPUT__ $VAR1 = bless( [ 1, 2, 3 ], 'XX' ); $VAR1 = bless( do{\(my $o = 'abc')}, 'YY' ); $VAR1 = bless( do{\(my $o = 123)}, 'ZZ' );
    You are right that state serialization only works when the state is entirely referenced through that blessed value. You could even say that it's breaking package encapsulation.

    But state serialization is, as the name implies, writing the state of the object, not the state of the package which manages the object. If you want to implement a new object model on top of the one Perl has got already, including flyweight or inside-out, then your new object model will have to be responsible for any serialization tasks. Unfortunately, that means that packages with non-native object models must document that they need special handling for serialization.

    --
    [ e d @ h a l l e y . c c ]

      Minor correction: object state serialization works for any blessed type in Perl, not just references to hashes.
      For what part of
      for instance, that objects are implemented using a reference to a hash, and that the entire state of the object depends only on the content of said hash (or reference to an array, or scalar).
      is that a minor correction?
      But state serialization is, as the name implies, writing the state of the object, not the state of the package which manages the object.
      An object in Perl is nothing more (and nothing less) than a blessed reference. Fly-weigth and inside-out objects are just that, blessed references. But in general, when people want to "serialize" an object, they want more than the reference, and the package it was blessed into. They also want whatever is used to keep the state of that object.
      If you want to implement a new object model on top of the one Perl has got already, including flyweight or inside-out,
      Fly-weight and inside-out objects don't implement a "new moduel on top of the one Perl has got already". What Perl has is bless REFERENCE, PACKAGE. Traditional hash based, fly-weight and inside-out objects use blessed references. Where fly-weight and inside-out differ from traditional objects is that traditional objects use the memory the reference is pointing to store their state, while fly-weight and inside-out use the reference as an index. But neither solution is any more or less "native" to Perl, as the language support for objects stops after bless.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://476859]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-07-13 17:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (250 votes), past polls