in reply to How to convert Storable object back to a hash

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;

Replies are listed 'Best First'.
Re^2: How to convert Storable object back to a hash
by tobyink (Canon) on Jun 21, 2023 at 05:48 UTC

    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