perlperlcc

Why is there a separate "perl" compiler and "perlcc" frontend for Perl?


I am trying to make an executable out of my Perl code and then I realized that there is no such option available with the perl compiler. After a bit of searching, I found perlcc, which is a frontend for the Perl compiler and does the job (produces a binary).

Why does Perl have a separate compiler and frontend? Like for example, gcc for C/C++ is a complete tool in itself. Is this just the way it is, or are there some good reasons behind it?


Solution

  • This answer was written a long time ago and doesn't reflect the current state. I'd rather delete this answer, but I can't since it's accepted. See Reini's answer instead.


    It's not typical for people to compile Perl programs to a binary. Plenty of people would like to do that, but that's just not the way it works. What are you trying to accomplish? There might be another way to do what you want to do.

    A Perl program really executes in two phases: a compile-time and a run-time. It's a dynamic language too, so you can't tell everything you'll need to compile at the end of the compile phase, and during the compile phase you might have to run some code.

    Perl is more like Java or Ruby than C in this manner. When you run a Perl program, the perl interpreter loads all the source code and compiles it into a abstract syntax tree. It's that bytecode that perl executes (or interprets) during the runtime. This is the same sort of thing that Java, Ruby, and Python do.

    One of Perl's warts is that it doesn't have a good way to save the result of that compilation, like those other languages can. That means you end up compiling the source every time. There are some ways around that with pperl and domain-specific tools such as mod_perl or fastcgi.

    There are some fuzzy bits in there with BEGIN blocks, eval, and so on, but that's mostly the way it works. There are more details in perlmod.

    This design wart wouldn't be there if we started all over with everything we know now, and indeed, in Perl 6 it isn't there. :)