Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by rinceWind (Monsignor)
on Jul 21, 2005 at 13:46 UTC ( [id://476829]=note: print w/replies, xml ) Need Help??


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

If you want to serialize in a human readable form, try YAML. Serialization of objects, be it with storable, Data::Dumper or YAML, will preserve the blessing, hence blessed objects are returned when you load them back.

Bear it in mind that loading an object twice will produce two different objects but with the same contents.

--

Oh Lord, won’t you burn me a Knoppix CD ?
My friends all rate Windows, I must disagree.
Your powers of persuasion will set them all free,
So oh Lord, won’t you burn me a Knoppix CD ?
(Missquoting Janis Joplin)

Replies are listed 'Best First'.
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
    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.

      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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://476829]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2024-03-28 09:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found