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

我的梦境 提交于 2019-12-01 07:54:51

问题


 HASH(0x991f0dc)

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.


回答1:


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.



来源:https://stackoverflow.com/questions/39227611/how-to-convert-the-perl-hash-string-reference-to-perl-hash-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!