rbala has asked for the wisdom of the Perl Monks concerning the following question:

If a change is made in the perl program during run time before that line is actually interpreted, why it is not reflected.? eg. In the below perl code,
$n1=5; #any numeric value $n2=10; #any numeric value $result = $n1 * $n2; print "$result";
while running the code, i changed the operator from * to + ie. $n1 + $n2.But still the result is for only multiplication. As far as I understand, no object output file is created during compilation of perl code and While running the code , each line is interpreted and executed before proceeding to next line. So, changes in any line of the code, before the line is interpreted, should be refleceted in output. Is my understanding correct ? Or is there any other way , execution of perl script happens ?

Replies are listed 'Best First'.
Re: Perl execution
by CountZero (Bishop) on Jul 05, 2012 at 06:44 UTC
    As far as I understand, no object output file is created during compilation of perl code and While running the code , each line is interpreted and executed before proceeding to next line.
    You understood that wrong.

    Perl is neither purely compiled; nor purely interpreted. It is a bit of both and neither.

    The perl executive reads the whole program and "compiles" it into an intermediate form, which then gets interpreted. Changing the source of your Perl script from within the program comes too late as your program has already been read in and compiled and it runs now out of its internal represenation which is utterly disconnected from the source text.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Not to mention the notion of "changing the source-code in mid-flight" is incredibly dangerous even if it did work.
Re: Perl execution
by Khen1950fx (Canon) on Jul 05, 2012 at 06:40 UTC
    Perl takes a "snapshot" of the currently running script, process, pid, etc. Any changes you make won't be recognized until the next run.
Re: Perl execution
by davido (Cardinal) on Jul 05, 2012 at 19:11 UTC

    ...each line is interpreted and executed before proceeding to the next line.

    Test your intuition:

    while( <DATA> ) { chomp; if( is_empty($_) ) { print "$.: ---\n" if is_empty($_); } else { print "$.: $_\n"; } goto JUMP if $. == 4; } sub is_empty { return ! length $_[0]; } BEGIN { print "Beginning execution.\n" } JUMP: { print "Look, found the JUMP block.\n"; } __DATA__ First line Third line Fourth line Fifth line

    So... the BEGIN{} block executes before everything else. So Perl must have known about it even though it appears physically lower in the file. The is_empty function is declared and defined after it is called, so Perl must have known about it when it executed the line "if( is_empty(...". And the <DATA> filehandle is being read even though its contents appear at the end of the file. And last but not least, goto found JUMP.


    Dave

Re: Perl execution
by locked_user sundialsvc4 (Abbot) on Jul 05, 2012 at 14:12 UTC

    And for what it’s worth, every such language (PHP, Python, Ruby, etc.) does something similar, with variations.   The source-code files are located and, if you will, “semi-compiled” into the data structures that actually drive the execution-engine.   The relationship between the two is very fluid:   you can on-the-fly eval certain text-strings, require certain files, and so on.   But there are still two (or more) distinct stages in the process ... getting from source-code to what is actually executed; and, executing that.

    Even a great many “pure compilers” do something very similar, because there is very little pragmatic overhead to this vis-a-vis “straight code,.” and very-appealing space savings.   Even the venerable Apple-][ computer had a “Sweet-16” p-machine as part of its Integer BASIC!

Re: Perl execution
by talexb (Chancellor) on Jul 05, 2012 at 18:18 UTC

    Does this surprise you? What are you trying to do?

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: Perl execution
by ig (Vicar) on Jul 06, 2012 at 10:20 UTC
    Or is there any other way , execution of perl script happens ?

    If you want to change your perl script at runtime you have several options including eval, the e modifier on regular expressions and running generated script in a sub-process.

Re: Perl execution
by Marshall (Canon) on Jul 07, 2012 at 00:06 UTC
    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.