I really like generating code on the fly. It is fairly easy if you watch your back(slashes):

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}->( @_);


In reply to Re: Code that writes code by mirod
in thread Code that writes code by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.