The Basics - Compilers / Interpreters


Before you can write Perl code, you need two basic things:

What exactly does the interpreter do? It depends on what you are looking to do. The Perl interpreter can function in a number of different ways. By nature it is a 'Hybrid Interpreter', although it has the ability to also function as a 'Pure Interpreter'. Perl programs can be interpreted one line at a time like a 'batch file' or 'shell script'. Perl programs can also be sent through a two stage interpreter that will produce 'JAVA' like byte code. This byte code can be immediately executed, or it can be sent to any one of the add on rear-end to the Perl system. The many rear-ends can convert the Perl code to C, JAVA, or many other forms.

There are also additional Perl modules that are capable of converting other forms of code into Perl. Languages such as 'sh', 'awk' and 'sed' can be converted to Perl.

Your first Perl program is much like programs you may have written in C++ or JAVA for the first time, in the form on a 'Hello World'. The Perl version of 'Hello World' is a lot different than the two major players offer, however. To create a 'Hello World' in Perl, all you need to do is type a single line (or a double, depending on which way you 'compile' the code.

Which is easier? Wouldn't life have been so much easier if you learned this in CMPT 183 / CMPT 505? Perl doesn't have many rules like the other major languages. The first rule is that you don't really need to 'end' a program. A Perl program will fall off the end of the code gracefully. Perl doesn't need a specific way to begin either, the only thing that may be required to start is the shabang, seen in the second example. As we will see later on, there are other pesky little rules that have been eliminated such as declaring a static length on an array, or even declaring variables altogether.

What is a shabang anyway? A shabang, to be exact, is a sharp (#) and a bang (!). The shabang is basically a way to specify where the Perl interpreter lives. There are two ways to do this, depending on which system you are using:

You may notice the -w after each shabang, this is simply a 'switch' to allow for extensive warnings. It is recommended that you use this when testing your programs, but remove it when you are done testing. It makes life a whole lot easier.

If you don't like the shabang notation, you can interpret Perl by using the interpreter directly. Again, it depends on what system you are on:

So now we have seen a very simple program, now to illustrate the rest of the basic features.



History    <Compiling>    Variables

Created By: Jason Zurawski
Last Modified: Feb. 29, 2004