http://qs321.pair.com?node_id=170664

jotti has asked for the wisdom of the Perl Monks concerning the following question: (cgi programming)

What happens when my CGI Perl script is called? Is every line interpreted every time the program comes to the line? F.i. if I have the following lines:

for (i = 0; i < 100; i++) { print "Hello, World\n"; }
...will the print line be interpreted 100 times? (I believe this was the case in old great interpreters like GWBASIC and QBASIC.) Or is the whole script compiled once every time it is called and then run like a compiled C++ executable CGI "script"?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: Are the scripts interpreted or compiled?
by Elian (Parson) on May 31, 2002 at 18:40 UTC
    Much as I like perl, this first answer is incorrect.

    Perl is certainly not the most perfect combination if interpreter and compiler. It's arguable whether there is one, but if there is then what we have now isn't it.

    While, strictly speaking, perl does have a compilation phase, transforming your source into an intermediate internal representation, it doesn't produce natively executable code. Producing that native code is what's generally associated with compilation. Instead it produces the intermediate code and walks through it, interpreting it.

    Given the current state of the art (at this point only a few shells are truly interpretive, not turning their code into some intermediate form) perl comfortably fits the definition of "interpreter"

    (Also, perl doesn't produce and interpret bytecode. It produces and walks an optree, which is a different structure altogether)

Re: Are the scripts interpreted or compiled?
by Juerd (Abbot) on May 31, 2002 at 10:53 UTC
    It is compiled on the fly. Perl is the most perfect combination of interpreter and compiler.

    Your example script would be compiled to bytecode first, and the bytecode would be executed. The bytecode generated by the print statement is executed 100 times.

    If you are concerned with efficiency, use for (1..100) instead of the C-style for.
Re: Are the scripts interpreted or compiled?
by Marza (Vicar) on May 31, 2002 at 18:00 UTC

    Depending on the need and the platform. ActiveState has Perlapp which will turn your script into an executable.

Re: Are the scripts interpreted or compiled?
by Jenda (Abbot) on Jun 11, 2002 at 15:10 UTC
    The fact that something is named *.exe doesn't mean it contains ONLY compiled code. The script is NOT really compiled by PerlApp. It's just packed up with some startup code, Perl interpreter, DLLs of the modules you need (if built as freestanding) and so forth. I do not think it's even compiled to bytecode before being stored in the EXE.