Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How to convert Storable object back to a hash

by cormanaz (Deacon)
on Jun 21, 2023 at 03:16 UTC ( [id://11153055]=perlquestion: print w/replies, xml ) Need Help??

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

When I store hash %foo using Storable I pass the hash as a reference. When I retrieve it, it comes back as an object $foo. Is there a way to convert this back to %foo? I've done some searching around in Super Search and Google and can't find an answer.

In case you're wondering why I'd want to do this because, it's because I use hashes to save typing the blasted -> operators (is that the right word?) you use to refer to object keys. If I write some script that is incremental -- i.e. I add some data to the hash and save, then retrieve and add more later -- I have to go back and convert the code to add the -> operators and I'd rather not have to do this.

  • Comment on How to convert Storable object back to a hash

Replies are listed 'Best First'.
Re: How to convert Storable object back to a hash
by swl (Parson) on Jun 21, 2023 at 03:27 UTC

    To convert a hashref to a hash you can dereference it. e.g.:

    my %hash1 = %{$href}; my %hash2 = %$href; my %subhash = %{$href->{subhash}};

    If you are using a recent perl then you might also consider postfix dereferencing. No examples as I have not tried it yet (not for any particular reason).

    And if you want an alias then use the refaliasing feature. This avoids making a copy of the hash (albeit a shallow one since perl copies the top level and keeps the inner references as-is).

    use experimental qw /refaliasing/; \my %hash = $href;

      No examples as I have not tried it yet (not for any particular reason).

      It's pretty simple:

      # Old-style dereferencing: my %hash = %$ref; my %hash = %{ $ref }; # New-style dereferencing: my %hash = $ref->%*;

      In most projects I use the old style because I'm familiar with it, and I often need compatibility with older Perls. I do agree that the new style is usually more readable (despite having more punctuation characters).

        I do agree that the new style is usually more readable

        For me, old style dereferencing makes a bit more sense in my head when reading the code. Mostly, because for me dereferencing reads more like a type conversion/math operation:

        # "The integer of $x" my $foo = int($x); # "The square root of $x" my $foo = sqrt($x); # "The reference of %x" my $foo = \%x; # "The dereferenced hash of $x" my %foo = %{$x};

        It also feels more in line with how variable types (scalar, hash, array) are typical denoted in perl, with the "classic" dereferenced hash starting with a correct %, whereas the new style feels more akin of "a scalar changing into a hash in the middle of the variable name".

        But that's just my personal preferences.

        PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
Re: How to convert Storable object back to a hash
by Discipulus (Canon) on Jun 21, 2023 at 07:13 UTC
    Hello cormanaz,

    maybe too early in the morning and low coffeine level still, but.. what's wrong with Storable#EXAMPLES?

    use Storable qw(store retrieve freeze thaw dclone); %color = ('Blue' => 0.1, 'Red' => 0.8, 'Black' => 0, 'White' => 1); store(\%color, 'mycolors') or die "Can't store %a in mycolors!\n"; $colref = retrieve('mycolors'); die "Unable to retrieve from mycolors!\n" unless defined $colref; printf "Blue is still %lf\n", $colref->{'Blue'};

    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: How to convert Storable object back to a hash
by bliako (Monsignor) on Jun 21, 2023 at 08:08 UTC

    Note the following from Storable documentation:

    Note that freezing an object structure and immediately thawing it actu +ally achieves a deep cloning of that structure:

    You are in for big surprises and bugs because you try to avoid the "blasted -> operators".

    bw, bliako

Re: How to convert Storable object back to a hash
by ikegami (Patriarch) on Jun 21, 2023 at 19:53 UTC

    You did get a reference (not a hash), but you did pass a reference (not a hash).

    Anyway, you can use

    use experimental qw( refaliasing declared_refs ); my \%foo = retrieve( ... );
    instead of
    my \%foo = retrieve( ... );

    This gives you the returned hash itself, instead of copying the returned hash into a new hash. It's faster, and it maintains reference cycles.


    Oops, I missed that someone had mentioned this.

Re: How to convert Storable object back to a hash
by Anonymous Monk on Jun 21, 2023 at 12:15 UTC

    How about

    my %hash = %{ retrieve( 'whatever' ) };

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-22 18:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found