PERL PRIMER 2.
TOPICS COVERED IN THIS PRIMER:
- Getting Perl.
- What version is it?
- Where is perl?
- Basic Script Layout.
If you have a UNIX based web host (Linux, *BSD, HP-UX, AIX OSX etc), chances are you already have Perl available to you.
If your host has a Windows server, it's also likely that Perl is already available to you.
If however you have your own Windows web server, or you want to set one up. This section is for you. http://www.activestate.com should be your first stop, there you can download a package called activeperl, it's a Windows MSI file, so save it somewhere, and double click on the downloaded file to start the install, then just follow the prompts.
For all other operating systems, see if your system comes with it already, or if not, head over to: http://www.perl.org/ and see if a version is available for your platform.
For Windows machines, I suggest you read primer 7 as it contains a great deal more information about installing Perl on windows machines.
WHAT VERSION IS IT?
Ok, your host has told you that Perl is available, the next step is to work out what version of perl you have. The current version at the time this primer was written is Perl 5.8.0 the best previous version was 5.6.1, but any version after about 5.0.3 is probably fine for most tasks.
The method you use to find out depends on your servers type and your access to it. If the machine is local, open a console prompt and type: perl -v or perl --version
If perl is installed it will promptly return the version number and some other details.
If the server is remote but you have telnet or ssh access, then the above also applies, just login to the server first and then type: perl -v
If you don't have telnet or ssh, and the server is remote, the only way that springs to mind to find out the version is to upload a test script, make it executable and run it. (I'll cover more on this later.)
Of course the best source of information will come from your hosting providers themselves, chances are they have a page on their site devoted to perl and CGI, you should look there first and possibly e-mail them about specifics.
WHERE IS PERL?
Running a Perl script is different in Windows then it is in Linux/UNIX. In Windows, the file extension .pl or .cgi (or both) is associated with the perl executable in the web server software and optionally to the system itself. In UNIX/Linux, the scripts first line will determine what interpreter will run the script. (see more about the shebang line later) On Windows this step isn't really necessary as it will ignore any file path on the first line anyway, but for UNIX scripts we need to know the location of the perl binary. So as detailed in "GETTING PERL" above, login to your server via ssh or telnet and type:
The server should immediately return the location of the perl binary on that system. the default is:
That file path must match the first line of any perl script for that server. It's called the shebang line. (see below for more).
Again, if you are not sure, and you have no SSH or Telnet access, then ask your ISP for the path to Perl. However the default listed above is usually correct.
BASIC SCRIPT LAYOUT.
A Perl script is just a text file, that's all and that's it. The contents of that text file is read by the Perl interpreter and converted to a "byte code" that the server understands, and subsequently executes. Doesn't sound to difficult right? well guess what? its not!
As I mentioned above, the first line of any Perl script on whatever platform, should be the path to the Perl binary interpreter. This is called the "shebang" line, and it looks like this:
#!/usr/bin/perlAny switches that you might want to add to pass to the interpreter are added to the end of the shebang line, here are a couple of examples.
Taint mode (tells Perl not to trust user supplied data):
#!/usr/bin/perl -TWarnings (Tells Perl to be verbose about error messages and possibly incorrect programming practices.):
#!/usr/bin/perl -wOr both:
#!/usr/bin/perl -wTAnything after this line is considered the body of the script and its made up of functions, declarations, loops and all the other usual suspects, all of which we will go into in the subsequent tutorials.
This site is totally free to use, you have absolutely no moral or legal obligations to help us continue.
There are however, some costs involved in running the site.
So if this site helped you find your way,
perhaps you could consider contributing to our costs.
Whatever amount you feel this site was worth to you would be just wonderful.
Use PayPal if you do decide to share and help us with the costs and in appreciation for our time and attention, or alternatively buy a book from our Bookstore..
Time in Don's part of the world is: Mon, March 30, 2015 at 02:03 AM
Time in Franki's part of the world is: Monday, March 30, 2015 at 3:03 PM
Don't worry neither one sleeps very long!