PERL PRIMER 5.
TOPICS COVERED IN THIS PRIMER:
This time I want to talk some basic stuff that you should know to start writing useful scripts quickly.
First off, I wanted to tell you that Perl is very easy to read.. it should be, the guy that "invented" it, is a linguist.. (Larry Wall) and he purposely wrote Perl to use the same conventions we use in everyday speech.
ok, so we know the first line of a Perl program should be: #!/usr/bin/perl The second line of CGI scripts as far as I am concerned should always be:
use CGI::Carp qw(fatalsToBrowser);
That line, put near the top of your script can save you hours of time debugging.. what it does, is try and print any errors to the browser screen instead of to the server's error log.. its much faster to see the mistake instantly then it is to have to go and look for it. very handy.. I go nowhere without it. :-)
The next thing that I always use isn't strictly speaking (excuse pun) necessary. .but its a very good idea anyway.
Using strict (called strictures) makes Perl "stricter" on you, it's normally very forgiving and if it can work out what you want, Perl will try and do it.. that can make for bad programming practices, and the longer the code, the harder it gets to work on, and the easier it is to create problems for yourself.
What's a Variable you ask?
my $variable1 = 'A boring string of text'; my $variable2 = 32; my $variable3 = ($variable2 + 34) * 10;
(It's not really relevant to this conversation, but $variable3 now has a value of 660)
Perl doesn't require that you pre-declair or scope your variables. (meaning that it doesn't ask that you tell it what a variable is going to be called and what sort of values it can contain.) languages like C, C++, JAVA C# all require that you tell them exactly what a variable is called and what it will do, and how big it will be.. before you use it.
Perl will quiet happily forgive you if you don't do it.. but you SHOULD do it, it makes programs faster, neater, use less memory and easier to work on if you do.. putting: use strict; near the start of a script tells Perl that you want it to force you to do it properly.. (it also helps to eliminate errors in your code by telling you about errors and stopping the script rather then leaving you wondering why the end result is not what you expected.)
Perl has a very simplified method of pre-declairing variables.. and of scoping them.
Here is an example.
#!/usr/bin/perl -T use strict; # engage strictures. my $path = '/var/www/cgi-bin/myscriptdir/';
Here we have two new concepts, a variable, and that "my" in front of it..
my $path = '/var/www/cgi-bin/myscriptdir/';
Here are the other two data construct types .. (information containers you could say.)
Arrays and hashes are list variables, meaning they contain more then one
my @array_of_animals = ( 'Dog', 'cat', 'horse', 'mouse', 'sheep', 'cow');
We have now created a list of animals and assigned them to a new array called @array_of_animals At any time you want, you can pop items out, (using a command called "pop" strangely enough :-) and you can push new items in, (using the "push" command, see what I mean about Perl using English conventions?)
The items in an array are numbered, so if you wanted to get to one variable in an array. (say horse in this case) you'd say:
my $amimal = $array_of_animals;
Notice two things, one I used $ instead of @ in $array_of_animals; that's
because we want a single value return, not a list. second thing is that I
put 2 in there instead of 3.. that's because arrays in Perl (and most other
languages) start at 0 not 1.. so Dog is 0, cat is 1, horse is 2 and so on.
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: Sun, March 26, 2017 at 09:52 AM
Time in Franki's part of the world is: Sunday, March 26, 2017 at 10:52 PM
Don't worry neither one sleeps very long!