in reply to Re: unblessed reference problem
in thread unblessed reference problem

Thanks james2vegas, that is definitely a very good solution to this specific problem. What I really want to know is how to get perl to do what I originally posted. I'm well aware some of the more esoteric solutions might be dangerous, but I still want to know how it works to further my knowledge.

Replies are listed 'Best First'.
Re^3: unblessed reference problem
by almut (Canon) on Mar 26, 2010 at 12:12 UTC
    but I still want to know how it works

    If you're referring to the eval thing, that would be:

    #!/usr/bin/perl -l use strict; use warnings; my $hash = { domain => { foo => { bar => "baz" } } }; my $domain = "domain"; my $var1 = "foo"; my $var2 = "bar"; my $test = '{$var1}{$var2}'; my $value = eval '$hash->{$domain}'.$test; print $value; # "baz"

    That said, better stay away from eval unless you know what you're doing, because

    • it's potentially dangerous when you overlook something (e.g. when the value of $test might originate from somewhere not under your full control)
    • code using it is unnecessarily hard to read and maintain
    • it's unnecessarily slow

      That's excellent stuff, thanks almut! Just what I needed to know.

Re^3: unblessed reference problem
by Anonymous Monk on Mar 26, 2010 at 11:55 UTC
    *sigh* To make $hash->{$domain}$test; into $hash->{$domain}{$var1}{$var2} you have to write $foo such that print $foo prints $hash->{$domain}{$var1}{$var2}, then you can eval $foo to execute.