Perl: Compiler or Interpreter?

by Philip Yuson

A compiler is a program that reads a source program and converts it into object code. This object code is then used by the Linker to produce an executable file. Both the object code and executable files are in binary form. This means that if you try to read them all you can see are garbage and some of the literals used in the program. The compile-link process ensures that the program is free from any syntax errors. It cannot determine logic errors in the program.

An interpreter is a program that reads a source program and executes the source program in real time. This cuts down the time to prepare the program as you do not need to compile and link the program. The interpreter executes the instructions as it reads the file. This however means that syntax errors can cause the program to abort in the middle of execution. This can be very frustrating and dangerous specially if your program has been running for quite a while.

Perl is a hybrid of the two. When a program is executed, perl reads the program and builds a binary file at the same time, checking for syntax errors. If there are no errors, it then uses the binary file to execute the instructions. Syntax errors will cause Perl to terminate BEFORE it goes on to execute the program. In a sense, Perl gives you the best of both worlds.

Another feature of Perl is that you can use it in interactive mode. This means that you can start perl and have it interpret your instructions as you type them.

To prove this, we will create a simple perl program. The program will have two statements.
(Note that you do not need to code the numbers on the right. I am using these numbers only to make it easier to explain the program.

To begin, you will have to start perl on your machine. Go to your command line and type perl

You will notice your command going up one line. This means that Perl has started and is waiting for you to enter your instructions. Type these two lines.
print "No errors\n";1
print :Error Here\n;2 (There will be an error message, don't mind it for now)
In MS-DOS, type CTL-Z and in Linux/Unix, type CTL-D to signify an end of file. Perl will then tell you that there was a compilation error and will terminate processing.

This is how your session will look like: This is how your session will look like:
Screen outputExplanation


print "No Errors\n";No Message
print :Error Here\n;
syntax error at - line 2, near "print :"Message due to error in previous line
^ZSignifies end of line
Execution of - aborted due to compilation errors.Execution failed because of compile error
Notice the following: If you will restart Perl and type the following lines:
print "No errors\n";1
print "Error Here\n";2
You will notice that perl executed the program only after it has finished processing all instructions. The result will look something like this: print "No Errors\n";
print "No Errors Now\n";
^Z
No Errors
No Errors Now
These two examples prove that Perl checks the program for syntax errors first and will only proceed executing the program if no syntax errors are found.

Note: Perl cannot know if you have any logic errors in your program. Logic errors are errors in the instructions that causes the program to work differently from what it was designed for.