I really like generating code on the fly. It is fairly easy if you watch your back(slashes):
- generate a string with the code for a subroutine (that's the fun part)
- generate an anonymous subroutine from it $sub= eval "sub { $sub_in_a_string }";
- use the subroutine $sub->( @args);
et voila!
Here is a stupid example:
#!/bin/perl -w
use strict;
# very simple rules: s="<string>" or v=<number>
my %rules=( 's="toto"' => sub { print "toto!\n"; },
'v=2' => sub { print "waouh! 2!\n"; },
);
# values to test
my @pairs=( toto => 2, toto => 1, tata => 2, tutu => 1);
my @rules= make_rules( %rules); # create the r
+ules
while (@pairs)
{ my $string= shift @pairs;
my $value= shift @pairs;
check_rules( $string, $value); # check all ru
+les
print "\n";
}
sub check_rules
{ my( $string, $value)= @_;
foreach my $rule (@rules)
{ my( $you_like, $do_it)= @{$rule};
$do_it->() if( $you_like->( $string, $value)); # run routine
+if rule applies
}
}
sub make_rules
{ my %rules= @_;
my @rules;
foreach my $exp (keys %rules)
{ my $sub_str= ' my( $s, $v)= @_;' ; # get the arg
+uments
if( $exp=~ m/^s=(".*?")$/) # s="<string>
+"
{ $sub_str .= "return 1 if( \$s eq $1);" ; } # watch for t
+he \,
# $s is a
+variable in the sub
# while $
+1 is a variable in make_rules and a constant in the sub
elsif( $exp=~ m/^v=(\d+)$/) # v=<number>
{ $sub_str .= "return 1 if( \$v == $1);" ; } # watch for t
+he \
else
{ die "syntax error in rule $exp"; }
my $sub= eval "sub { $sub_str }"; # create the
+anonymous sub
push @rules, [$sub, $rules{$exp}]; # store it wi
+th the sub to run
}
return @rules;
}
You can see an example in Ugly XML processing looking for a pure XML solution: I create a hairy regexp
in make_wrapper, which is used in wrap
-- calling the wrapper in wrap should really be written
$wrapper{$tag}->( @_);
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.