In print_story I would like to have something simple like, but I doubt it will be that simple.
Sure, it can be that simple, though I might feel safer trying to avoid arbitrary code execution with Safe like
#!/usr/bin/perl --
use strict; use warnings;
use Safe;
my $str = <<'__STR__';
&print_definitions( file => "some_file_a.txt", headings => ["term","de
+finition"],)
&print_definitions( file => "some_file_b.txt", headings => ["term","de
+finition 1","definition 2"],)
&f()
&f ( 1 )
&eff( 'a', "A\tB", 3 )
__STR__
my $namespace = __PACKAGE__;
my %dispatch = (
eff => \&f,
_default => sub {},
);
open my($in), '<', \$str;
while( my $line = <$in> ){
if( my( $sub, $args ) = $line =~ /^\&([^\(\s]+)\s*(.*)/) {
print "## $sub $args \n";
my $subref = $namespace->can( $sub );
$subref ||= $dispatch{$sub} || $dispatch{_default};
if( $subref ){
$subref->(
length $args
? Safe->new->reval( $args )
: ()
);
}
}
}
close $in;
sub print_definitions { print "print_definitions says [ @_ ]\n\n"; }
sub f { print "f says [ @_ ]\n\n"; }
__END__
## print_definitions ( file => "some_file_a.txt", headings => ["term"
+,"definition"],)
print_definitions says [ file some_file_a.txt headings ARRAY(0xb0b93c)
+ ]
## print_definitions ( file => "some_file_b.txt", headings => ["term"
+,"definition 1","definition 2"],)
print_definitions says [ file some_file_b.txt headings ARRAY(0xb0b5ec)
+ ]
## f ()
f says [ ]
## f ( 1 )
f says [ 1 ]
## eff ( 'a', "A\tB", 3 )
f says [ a A B 3 ]
Does that clear things up for you? |