Here, we defined $sendtomail to firstname.lastname@example.org.
After defining the scalar, the programmer can use $sendtomail anywhere into
their code -- and when the program is run, wherever $sendtomail appears,
email@example.com will be inserted.
You can easily identify a scalar variable in a program, because it always starts with a dollar sign (eg:
$sendtomail) and has a definition (e.g.:
Continuing with the above example, let's say the program sends an email to the email address defined in
$sendtomail. The following line tells the program who the email is to.
print MAIL "To: $sendtomail\n";
Later in the program, the programmer can also use:
print "Thank you for your input! Your message has been sent to
Variables, in essence allow you to set a definition once, and use it as many times as you need to.
If you've installed perl programs before, you probably have already seen variables in program setup
(configuration) section. These configuration settings are usually either in a file by itself, or at the
top of the main program file.
By having a group of configuration settings, the programmer makes it very easy to distribute the
program to many different people.
Defining scalars follows a specific format. The dollar sign immediately tells the program that it's
a scalar. Immediately following the dollar sign is an alphanumeric string (never starting with a
number), then an equals sign (=), then the definition, and last, the semi-colon (;). Put all together,
it looks like:
$sendtomail = 'firstname.lastname@example.org';
Notice that in my example, the email@example.com is enclosed in
single quotes ('firstname.lastname@example.org'). Sometimes you'll see a scalar
definition enclosed in double quotes ("admin\@perlarchive.com").
Wait a second, where did that backslash (\) come from? And at other
times, you won't see quotes at all ($total = $subtotal * $taxrate). Uh
oh… (There are others, too, but they're used for actual programming and beyond the scope of this article.)
Because Perl is such a robust language, it takes into account the different types of data
you want to store in your scalars. Let's start with text.
Text is any alphanumeric string which can also include any special character
(~!@#$%^&*()_). Because perl uses some of these special characters (such
as the $ that goes in front of a scalar), we need to tell perl "Even
though there's a special character here, don't do anything special with it - just display it!".
3 of the most common special characters perl uses to define/retrieve data:
$ tells perl it's a scalar
@ tells perl that it's an array (in part 2)
% tells perl that it's a hash (in part 3 Next issue)
Now that we know this, we just need to know how to tell perl not to interpret these special characters.
There are 2 ways:
- Enclose the scalar definition in single quotes. The single quotes tell perl to treat the string
literally, and not to interpret it.
$sendtomail = 'email@example.com';
- If using double quotes, escape the offending characters with a backslash
$sendtomail = "admin\@perlarchive.com";
Well, what if you wanted to use a single quote (aka apostrophe ') inside a scalar definition that's
enclosed in single quotes? Easy, just escape the apostrophe using a backslash.
$blah = 'It\'s a perl scalar variable.';
The single (or double) quotes are at the beginning and at the end of the definition to mark the start
and end of the definition. If you do not escape the repeated quote
('It's a perl scalar variable.') or double quote
("He said "a what?""), perl will think you're done where the apostrophe (or
double quote) appears. So what do you think will happen if perl thinks that
$blah = 'It'
is your complete scalar and
s a perl scalar variable.';
is just hanging out at the end?
Ha! Crash and burn it will, and produce a lovely "Internal Server Error", a completely misleading error
message which just means that perl didn't like something in the code.
Numbers are easy. They typically don't even need to have any quotes around them, even when performing
$month = 12;
$total = $subtotal * tax;
If, however, you wanted a full, formatted date (9/1/1999), it would need to be treated as "text". The
slashes would simply tell perl to performs multiple divisions.