Maybe I will get in to trouble for showing you something this goofy.

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.

#!/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
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.

In reply to Re: Perl execution by Marshall
in thread Perl execution by rbala

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.