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 (Canon) 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 (Canon) 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!