Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Store only object data but not the whole class

by exilepanda (Pilgrim)
on Aug 30, 2016 at 10:38 UTC ( #1170776=perlquestion: print w/replies, xml ) Need Help??

exilepanda has asked for the wisdom of the Perl Monks concerning the following question:

package Whatever; sub new { bless { foo => 'bar' }, shift; } 1; package main; use Storable; my $obj = new -> Whatever; store $obj , "somefile.sto";

Given a condition like the code above, the Storable file will store the data structure with it's class. But what I want to do is only store { foo => 'bar' } without the blessed ref.

I can actually do this with unbless from Data::Structure::Util qw( unbless );

However, I remember I did get this job done with a simpler way yrs ago without using module. Seem I had played with of sort of de-referencing trick, but I really forget how to make that up anyway. Do you know how?

Replies are listed 'Best First'.
Re: Store only object data but not the whole class
by salva (Abbot) on Aug 30, 2016 at 10:41 UTC
      GREAT!!! Thaz exactly the piece of code I am looking for !! Thx a lot man! XD
Re: Store only object data but not the whole class
by Discipulus (Abbot) on Aug 30, 2016 at 10:57 UTC
    If performance is needed you can be interested in Sereal and to this thread: Storing state of execution

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Store only object data but not the whole class
by NetWallah (Canon) on Aug 30, 2016 at 13:56 UTC
    FYI - when run, your posted code displays:

    Can't locate object method "Whatever" via package "new" (perhaps you forgot to load "new"?) at -e line 9.

    The object creation should be (my preferred syntax):

    my $obj = Whatever::->new();

            "Software interprets lawyers as damage, and routes around them" - Larry Wall

      ... object creation ... (my preferred syntax):

      my $obj =  Whatever::->new();

      Just as a matter of curiosity, why do you prefer the trailing  :: in the constructor call?


      Give a man a fish:  <%-{-{-{-<

        "Just as a matter of curiosity, why do you prefer the trailing :: in the constructor call?"

        There's a discussion about this in "perlobj: Invoking Class Methods".

        — Ken

        The "::" makes it clear that the "Whatever" is a package name. Other possible barewords could be "sub", or "File handle".

                "Software interprets lawyers as damage, and routes around them" - Larry Wall

      Ouch.. sorry for my finger slipped, that  -> was obviously unwanted. that should be new Whatever(); ( for my very old school fashion ). Thanks for point out!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2020-02-29 04:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (128 votes). Check out past polls.

    Notices?