I can't think of any reason to do this other than this demo. What this program does is read itself into a $variable, then it modify that copy of itself, and then it runs that modified code via an eval. The eval causes the modified copy to be compiled and run. So result is 50 on the first run and 15 on the "second" run!
There are FAR better ways of doing this! If you could explain the problem that you are trying to solve, I am sure the Monks can come up with a solution.
One place where the ability to compile things during program execution that is sometimes very useful is in regex'es. Unlike most languages where you have to make a regex that is "static" and cannot be changed after the program is compiled into the .exe, with Perl you can "generate a new regex on-the-fly". I have one program that does this to stunning effect. This program looks for "sort of matches" according to a very application specific algorithm. Instead of having some super-duper "regex from hell" with lots of backtracking and "ya, but's", I dynamically create the search regex in a very simple way based upon the search term. It is easy for regex engine to optimize it very efficiently. Then I run that program generated regex on maybe 80,000 things. Of course it takes time to generate and compile this regex, but in this application it is fine because I use it a heck of a lot of times.#!/usr/bin/perl -w use strict; use Data::Dumper; my $n1=5; #any numeric value my $n2=10; #any numeric value my $result = $n1 * $n2; print "$result\n"; #__END__ seek (DATA,0,0); my $program = do { local $/; <DATA> }; $program =~ tr/*/+/; $program =~ s/#__/__/; #prevent infinite loop eval $program; __DATA__ =prints 50 15 =cut
In reply to Re: Perl execution
by Marshall
in thread Perl execution
by rbala
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |