Instead of just evaling the code, wrap the code in an anonymous sub, thus capturing it so you can resuse it. So we have a routine called parse_to_actions that builds a bunch of lines of perl statements that need to be executed. Then we do this:
Similar to what xmath posted, but building the subs dymacially.my %code_cache; while (<>) { my $code=$code_cache{$_}; unless ($code) { my @actions=parse_to_actions($_); $code=eval "sub { @actions }" or die "$@ while evaling actions @actions "; $code_cache{$_}=$code; } $code->(); }
However when you consider that we can define parse_to_actions to return a sub, then we could
---use Memoize; sub parse_to_actions { return eval "sub { @lines_of_code }" or die $@; } memoize("parse_to_actions"); while (<>) { #Parse and generate. Memoize caches. parse_to_actions($_)->($_); # pass the line to the generated sub # just in case it gets smart }
In reply to Re: Re: Re: Caching process sets
by demerphq
in thread Caching process sets
by billyak
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |