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.
#include <iostream.h>
int main(void)
{
cout<<"Hello World"<<endl;
return 0;
}
public class HelloWorld {
public static void main (String args[]) {
System.out.println("Hello World");
return;
}
}
print "Hello World!\n";
OR
#!/usr/local/bin/perl -w
print "Hello World!\n";
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:
which perlYou can also try:
whereis perlThe output should look something like this:
/usr/bin/perlOR
/usr/local/bin/perlThese locations are what you need to place after the shabang:
#!/usr/bin/perl -wOR
#!/usr/local/bin/perl -wNow you are ready to go, almost. To run your program you first need to change it's permissions. The command chmod is what you need:
chmod 700 filename.plThis will change the file into:
-rwx------In *NIX terms, this means you (and only you) can read, write, and execute the file. The most important part here is 'execute', because that is what we need to do to the file to see it in action. Now you can do this:
./filename.plOR
filename.pl
#!c:\>perl\bin\perl.exe -wAfter you have installed a program such as ActivePerl, it will then be possible to simply 'click' on a Perl file to run it (provided that you use the proper file extension and the interpreter is installed the right way).
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:
perl filename.plOR
/usr/local/bin/perl filename.plUse the which command if you need to know the full path.
c:\>perl.exe filename.plOR
c:\>perl\bin\perl.exe filename.plAlso bear in mind that the 'filename.pl' needs to be in the proper place, or you will need to add a full path to it as well!
So now we have seen a very simple program, now to illustrate the rest of the basic features.