FIXIT MENU:
home about us contact us

WHAT'S AVAILABLE:
free scripts advanced scripts online tools great books web related tutorials contributed tutorials news archive geek toys!

SUPPORT:
help forum live chat help

PERL PRIMER 5.



TOPICS COVERED IN THIS PRIMER:
- The world though Perls' eyes.
- Variables, what they are, what they do, and how you use them in Perl.


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.

use strict;

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?
A variable is essentially a named placeholder or container, you can put anything you want in a variable. (called "assigning" a value to a variable). Variables can hold numbers (integers, floats etc), strings of text and more complicated data constructs.
Here are some examples of simple use of variables.:

     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)
These examples are all "scalar" variables, they contain only one value or pointer.
All programming languages use variables, and you won't get anywhere writing CGI scripts without them.
There are other types of variables as well, namely "arrays" and "hashes", but we will discuss them later in this primer or the next one.

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..

Perl has two main ways of declaring a variable and like I said, the guy's a linguist.. so the ways are:

- "my" (for private variables that only exist in the block of code they were declared in)
- "our" (for declaring variables that will be global in scope, meaning that the whole script can see them regardless of what block of code they were declared in.) so this line:

my $path = '/var/www/cgi-bin/myscriptdir/';

Means this:
Here is a variable called $path, it should only exist in this existing code block, and it should contain the value: "/var/www/cgi-bin/myscriptdir/" (sans the quotes)
So we declared the variable (told Perl about it), scoped it (told Perl where to use it) and assigned a value to it,, all in one line.. pretty neat huh???

If you didn't put the "my" in front of it, Perl would have complained about it, (if you had "use strict;" at the start of the script.) You only put 'my' before a variable the first time you use it,, after that perl already knows about it so you don't need to do it again in that block of code. Now there are other types of data structures as well, this is not confined to Perl, all languages use these, even javascript.

Here are the other two data construct types .. (information containers you could say.) They are:
Array
and
hash

Arrays and hashes are list variables, meaning they contain more then one value.

An array is a basic list. here is an example.

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[2];

Notice two things, one I used $ instead of @ in $array_of_animals[2]; 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.

__END__


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.

<random humor>
Plus Franki is trying to keep his boat floating.
</random humor>

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:     Wed, April 24, 2024 at 05:43 AM
  Time in Franki's part of the world is:     Wednesday, April 24, 2024 at 6:43 PM
 Don't worry neither one sleeps very long!



privacy policy :: support us :: home :: live chat help
contact us :: forum ::tutorials :: bookstore :: Site Map



      Valid XHTML 1.0!             powered by Apache Server
Pic 3 Pic 3

SEARCH:
USEFUL LINKS:

CIGHTML Firefox Thunderbird ClamWin WordPress SpyBot S&D TheGIMP Apache for Windows Registry Cleaners More cool stuff: