Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Code that writes code

by mirod (Canon)
on Dec 23, 2000 at 00:13 UTC ( #48086=note: print w/replies, xml ) Need Help??


in reply to Code that writes code

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://48086]
help
Chatterbox?
LanX he he

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2018-07-20 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (428 votes). Check out past polls.

    Notices?