in reply to Re: How to interpret a string as perl code and execute it?
in thread How to interpret a string as perl code and execute it?
Thanks; string eval does work in the example. But the following bit more complex example, in which the string needs to use the "context" of the executing script, fails:
use diagnostics; use warnings; use strict; my %h; $h{ 'a' }{ 'b' } = 5; my $b_code =<<'CODE'; sub tryme_b { my $foo = shift; return ${ $foo }{ 'a' }{ 'b' }; } CODE my $ans = eval "$b_code; tryme_b( \%h );" or die "[$b_code] doesn't wo +rk:$@"; print "b:$ans,\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: How to interpret a string as perl code and execute it?
by Corion (Patriarch) on Feb 05, 2011 at 17:12 UTC | |
|
Re^3: How to interpret a string as perl code and execute it?
by ikegami (Patriarch) on Feb 05, 2011 at 17:16 UTC |