http://www.perlmonks.org?node_id=1001613

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

Hi All,
my $var = {A => 1}; my $string = "$var"; print Dumper $string;
OP ---
$VAR1 = 'HASH(0x81b2818)';
Caught up in the scenario where we need to pull hash from string 'HASH(0x81b2818)'.

Replies are listed 'Best First'.
Re: Reading from memory location
by BrowserUk (Patriarch) on Oct 31, 2012 at 07:27 UTC

    Why have you stringified the hash reference in the first place?

    Whilst converting the string back to a hash reference is theoretically possible; it is difficult to do; and fraught with enough dangers that it should be avoided, even at great cost. And in general, it can be avoided without great cost.

    If you could explain why you are stringifying the reference in the first place, we can probably suggest alternatives that would avoid the problem.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Re: Reading from memory location
by Anonymous Monk on Oct 31, 2012 at 07:34 UTC
Re: Reading from memory location
by Happy-the-monk (Canon) on Oct 31, 2012 at 07:20 UTC

    $VAR1 = 'HASH(0x81b2818)';

    Try a backslash before the sigil: print Dumper \$string;

    Cheers, Sören

      Actually trying it might have been a good idea.

      >perl -wMstrict -MData::Dumper -le "my $var = {A => 1}; my $string = qq{$var}; print Dumper \$string; " $VAR1 = \'HASH(0x3cc06c)';
Re: Reading from memory location
by marquezc329 (Scribe) on Oct 31, 2012 at 07:32 UTC

    hello prashantpal84. In the future, your questions are more easily read if you enclose source in <code> tags.

    i.e.

    my $var = {A => 1}; my $string = "$var"; print Dumper $string;

    Output:

    $VAR1 = 'HASH(0x81b2818)'

Re: Reading from memory location
by space_monk (Chaplain) on Oct 31, 2012 at 11:43 UTC

    Why don't you simply:

    use Data::Dumper; my $var = {A => 1}; print Dumper $var;

    Output:

    $VAR1 = { 'A' => 1 };
Re: Reading from memory location
by Anonymous Monk on Oct 31, 2012 at 07:21 UTC

    Caught up in the scenario where we need to pull hash from string 'HASH(0x81b2818)'.

    Sorry, but there is no such scenario