Tinkering with Perl

(Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  18  19  20  21  22  23  24  25  26  27  Next  Skip Forward
Printer-Friendly Version

Scalars

A scalar is a variable that can either refer to a number, or some text, which is called a string. A string is usually enclosed in quotes, like this:

"This is a string."

Note that the quotes are not actually part of the string; they are put around the string to tell when the string begins and ends.

You can give a variable almost any name that you can make from letters, numbers, and underscores ('_'). Furthermore, you must put a dollar sign ('$') before a scalar to tell Perl that it is a scalar.

In general, it is a good idea to have a variable name consist of a few words that describe what the variable tells you. There are a couple of ways people have of putting words together. (You have to do something to tell when one word ends and the next begins, because itishardtoreadwhenyoucan'tseparatewords.) One way is to capitalize the first letter of each word; another is to separate words using underscores ('_'). It doesn't matter which way you do it, but you should pick one way and stick with it. It is very important that you spell a variable exactly the same way every time you use it; otherwise, the computer will think you are using different variables. Here are some examples of good variable names:

$NumberOfTrucks
$AverageHeight
$PlayerName

Or, if you prefer underscores, then you can do it this way:

$number_of_trucks
$average_height
$player_name

In this book, I will always do it the first way.

See also:

Variables - Lists - Hashes - Assignment of variables in general - Assignment of scalars - Arithmetic

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

Read more...

Top

(Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  18  19  20  21  22  23  24  25  26  27  Next  Skip Forward
Printer-Friendly Version