sg has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
If a perl script gets access to a string, how can the script interpret the contents of the string as perl code and execute it? My naive attempt failed:
use diagnostics; use warnings; use strict; my $a_code =<<'CODE'; sub tryme { my $foo = shift; return $foo + 2; } CODE # Intent shown here does _not_ work my $ans = eval{ $a_code; tryme( 3 ); } or die "doesn't work";
The error might be in the way in which the code in being packaged into the string $a_code, or in how $a_code is being used, or in both.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How to interpret a string as perl code and execute it?
by Corion (Patriarch) on Feb 05, 2011 at 16:28 UTC | |
by sg (Pilgrim) on Feb 05, 2011 at 17:03 UTC | |
by Corion (Patriarch) on Feb 05, 2011 at 17:12 UTC | |
by ikegami (Patriarch) on Feb 05, 2011 at 17:16 UTC |
Back to
Seekers of Perl Wisdom