Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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 ( #476956=note: print w/ replies, xml ) Need Help??


in reply to Re^2: 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?

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 ]


Comment on Re^3: Can I serialize an object and have it remember it's type?
Download Code
Re^4: Can I serialize an object and have it remember it's type?
by Anonymous Monk on Jul 22, 2005 at 12:43 UTC
    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://476956]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-12-20 03:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls