a compiler for perl

Jecel Assumpcao Jr jecel@tunes.org
Fri, 19 May 2000 13:37:53 -0300


On Fri, 19 May 2000,  Siddhartha Jain wrote:
> I really don't know if this is the right place for this posting but anyway 
> here it is.

It is the right place in the sense that here you will find people who
can answer your question correctly. This forum is meant for discussion
of more advanced computing concepts, but sometimes the most basic
questions can lead to deep insights and so shouldn't be ignored.

> Its about can you have a compiler for perl or for that matter 
> any scripting language?

The simple answer is YES. But there is one expression in many scripting
languages that can't be compiled - I'll explain below.

> My understanding is that since scripting languages don't have to declare the 
> variables to be used beforehand so the scripts cannot be compiled because 
> the compiler wouldn't know how much space to allocate and other similar 
> memory management related problems.

The compiler could look at the whole source in an initial pass to
gather that information. Of course, if the language allows you to
assign different data types to the same variable:

           x = 1
..
..
           x = 1.0

then you would have a problem if integers and floating points take up
different amounts of memory or need to go into different registers. One
solution that some languages use for the first problem is to store
pointers (which are always the same size) to the actual data in the
variables, and the data itself in a dynamically allocated heap. The
solution to the second problem is easy - don't store variables in
registers ;-)

Note that FORTRAN was one of the very first compiled languages and it
didn't use declaration of variables (though the variable type was
implied by the first letter of the name: i to m for integers, floating
point otherwise).

> So first am i right? If yes, are there other reasons too why scripts cannot 
> be compiled.

If you have an interpreter, then there is this nice and useful piece of
code sitting there right in memory beside your own script. It seems a
shame not to be able to use it, so many interpreted languages do allow
you to invoke the interpreter using an "eval" or similar function. Now
we can write something like this:

        print "Type in any expression"
        read $exp
        print "The result is", eval($exp)

Note: this isn't any particular language, but easily translatable to a
number of them. Since the compiler doesn't know what expression the
user will type in at run time, it can't possibly generate code for this
(other than include a whole interpreter and compile a call to it).

> I was going thru' perl-howtos and i found a link for compiling 
> perl scripts. i didn't follow the link but it was supported by only Perl4 
> not Perl5.

As long as you stay away from eval, Perl is no harder to compile than C
or any other language.

Now the complex answer: depending on what reflective facilities a
language offers, the job of a compiler can become very hard indeed!
For example, programs in LISP have access to their own code and can
even change it while running.

-- Jecel