Jonathan's Corner
(Sitemap)
> Orthodox Books Online, and More >
Technology >
Tinkering with Perl
Skip Back
Previous
22
23
24
25
26
27
28
29
30
31
Next
Skip Forward
Printer-Friendly Version
In math class, with a story problem, your teacher might have said something like this: "Suppose we have five cats..." What is going on here is that we have some number of cats, and we are saying that the number of cats is specifically equal to five.
Another way of saying what the teacher said is,
Let the number of cats be five.
We can use a variable for the number of cats. Let's put a variable in there:
Let NumberOfCats be five.
Or, to say it a little differently,
Let NumberOfCats equal five.
With computers, we drop the 'let', even though it's understood.
NumberOfCats equals five.
(Now, we are not simply claiming that the number of cats equals five. We are commanding that it be so.)
Finally, in Perl, we use an equals sign ('=') when we mean "equals", and we use numerals: we write '5' instead of "five". And remember — most statements end with a semicolon, and we put a dollar sign ('$') in front of scalars. So let's change the period into a semicolon, and put a dollar sign in front of the variable:
$NumberOfCats = 5;
And that's how we do it in Perl. We have just assigned the variable NumberOfCats a value of five.
We can also assign a variable to other things. For example, if we know the number of cats, and we know the total number of cats and dogs, we can find out the number of dogs by subtracting. If we have thirteen cats and dogs total, and five cats, then here is how we can get the number of dogs:
$NumberOfCats = 5; $NumberOfCatsAndDogs = 13; $NumberOfDogs = $NumberOfCatsAndDogs - $NumberOfCats;
The computer has done the subtracting for you, and figured out the answer.
Note: In an assignment, there is one variable on the left side of the equals sign, which is changed. Nothing on the right side (unless it is also on the left side, which will be discussed later) is changed.
Statements - Assignment of scalars - Assignment of lists - Arithmetic - Functions
Tinkering with Perl is a free book that provides an introduction to programming in Perl, as well as a basic reference for things like foreach in Perl, if-then, and if-then-else, in addition to providing a glossary where you can find definitions for concatenate and other terms.
Tinkering with Perl may be one of the most popular offerings on this site, but it's not the only attraction. You can read a tongue-in-cheek Game Review: Meatspace, read an even more offbeat customer service survey (whether or not you actually fill it out), and spend a few minutes wishing your boss would read, The Administrator Who Cried, "Important!" (Not to mention that there are other things you can read here besides tech stuff, from Janra Ball: The Headache to The Spectacles.)
Jonathan's Corner
(Sitemap)
> Orthodox Books Online, and More >
Technology >
Tinkering with Perl
Skip Back
Previous
22
23
24
25
26
27
28
29
30
31
Next
Skip Forward
Printer-Friendly Version