How To Run A Perl Script:
Knowing perl syntax is a bit useless if you cant execute your code, so here's a guide.
If you're using any variation of unix (so that includes mac osX and just about any linux) then you should have perl already installed, in which case you can skip the next part.
If you are using windows, then you will need to download and install
activeperl.
Once you have perl you can execute code from the command line, the usual command would be >perl yourfilename.pl
the initial perl, tells the system to run the file in the perl compiler, and then the filename can be either just the filename (if it is in your current directory) or the full path to the file.
Perl script can be written in any simple text editor, just be sure to save them with a .pl extension.
On To Some Code:
As is usual we'll begin with the customary 'Hello World!'.
Now for the minimalists among you, this code will print 'Hello World!' to the command line.
| Code: |
print 'Hello World!';
|
Now those of you used to languages such as C or C++ may be surprised to find that that will compile and run nicely, it is however more usual to start your programs with the line
this line confirms where the perl compiler is installed, and while it isn't necessary many perl programmers will have it at the start of their programs.
Another handy line to have at the start of your programs (especially while learning) is
This turns on some more friendly warnings if you make a mistake which are a bit more understandable than the gobbledygook the compiler sometimes throws at you.
Of course just printing words to the screen isn't very useful, lets see what we can do with variables.
All scalar (that is, single value) variables in perl begin with '$'. You do not have to specify whether this needs to be an int, long, string etc. perl works that out for you depending on how you use it.
This is part of what makes perl so easy to use as it will also change the type of variable if it needs to to make it fit the context you use it in.
Other variable types in perl include arrays and hashes but I will go into those in other tutorials.
For other reading on perl visit http://www.perl.org/.