in reply to Embedded Syntax Checker?
#!/usr/bin/perl -Tw use strict; my @code = ( 'for(my $i=0;$i<10;$i++) { print $i," "; }; print "\n";', 'blarmityschlammit', 'for(my $i=0;$i<10;$i++) { print $i,"\n";', # Missing closing brace '}; print "code4 sneaky code\n"; sub { print "code4 regular code\n";' ); for my $i (0..$#code) { print "\n** Compiling code block $i\n"; my $user_code = eval "sub { $code[$i] };"; if ($@) { warn "Bad user code block: $@\n"; next; } print "** Running code block $i\n"; $user_code->(); }
As the last example demonstrates, it's possible to break out of this and execute code at compile-time, but it's unlikely to happen accidentally. If you don't have malicious code, something like this will probably work OK. If you do, as merlyn said, you'll need to use the Safe module.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Embedded Syntax Checker?
by Anonymous Monk on Apr 21, 2004 at 16:22 UTC | |
by dragonchild (Archbishop) on Apr 22, 2004 at 15:26 UTC | |
|
Re: Re: Embedded Syntax Checker?
by flyingmoose (Priest) on Apr 21, 2004 at 19:06 UTC |