dhana govidarajan dhana govidarajan - 1 year ago 52
Perl Question

how to convert the perl hash string reference to perl hash reference


so the above hash reference is stored as string in scalar variable,if we
dereference it with "%" in the Perl program it throws error,

Can't use string ("HASH(0x991f0dc)
") as a HASH ref while "strict refs" in use at tcpclient1.pl line 49, <GEN0> line 1

this above is throws from Perl program,i request solution,to convert back the hash string reference to Perl hash reference.

Answer Source

You can't convert this back.

The HASH(0x991f0dc) is not a hash reference. It is the string representation of a variable that contains a reference to a hash at that address. The error message you describe when you deref it points to that you have something similar to this:

my $foo = 'HASH(0x991f0dc)';
print %$foo;

Now that won't work, because the actual data structure is not there.

It looks like you got this over a socket that you read from through the GEN0 filehandle. Whoever sends you that data structure is doing something wrong.

They need to serialize the data, and then you need to deserialize it back. A good way to do that is JSON. But you could also use Storable, or Sereal.