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


in reply to Re^2: How to interpret a string as perl code and execute it?
in thread How to interpret a string as perl code and execute it?

Would your code work when run stand-alone? Have you printed out the code you're trying to eval? Have you looked at the error message and looked at what causes it?

The following change to your code might help you see the problem:

use diagnostics; use warnings; use strict; use Data::Dumper; my %h; $h{ 'a' }{ 'b' } = 5; my $b_code =<<'CODE'; sub tryme_b { my $foo = shift; return ${ $foo }{ 'a' }{ 'b' }; } CODE my $c = "$b_code; tryme_b( \%h );"; my $ans = eval $c or die "[$c] doesn't work:$@"; print "b:$ans,\n";

Also, basic debugging in your tryme_b would also show you what goes wrong.