Perl Crash Course: Scalars

Share This Post

Scalars

Variable Names

Scalar is the type of variable used to hold one single piece of information. Examples of scalar information are one string, one character, one object, one reference… If you can say one something, then it’s scalar.

All scalar variables start with $ and a letter or underscore. As we will see in the Regular Expressions chapter, there can be variables such as $1, $2, etc. but they are reserved by the system. $0 also exists and keeps the name of the script. The size of variable names is limited only by your imagination and common sense.

$var
$_thisName
$a_really_big_and_annoying_variable_name

Quoting, Escaping, and Interpolation

When assigning a string to a variable, you can include other variables and count on getting their values. This is called interpolation, and can be achieved by using double quotes (” “) around your string.

$name = 'Vinny';
$some_string = "My name is $name\n"; # My name is Vinny

In the previous example, Perl sees that we are using double quotes, and also sees that there is a variable ($name) inside the string. So first it replaces the variable with its value and then assigns the interpolated string to the variable named $some_string.

Had we tried the previous example with single quotes, the result would have been literally My name is $name. Note that we also added a “\n” in our double-quoted example, which means to go to the next line. Newline characters (n), as well as everything else is interpreted literally when using single quotes.

Interpolation can be tricky at times when we want no spaces between our variable and some part of the string, or when we want to print a variable name within a double-quoted string. The solution for this is the “” (backslash), which we saw just above. Backslashes are used for escaping and giving special meanings to certain characters.

For example:

$some_string = "My name is \$name\n"; # My name is $name\n

Since there’s a backslash just before $, it tells Perl to NOT replace the variable $name with its value. It’s simply using the literal variable name. We could have achieved just about the same results if we had surrounded the string in single quotes, with the drawback of having a literal n at the end of our string:

If you need to use a single quote inside a single-quoted string, you can escape it with the backslash as well:

$hello = 'Welcome to usestrict\'s Perl Crash Course';

Follows a list of Perl’s backslashed character escapes:

Code Meaning
\n Newline (usually LF)
\r Carriage return (usually CR)
\t Horizontal tab
\f Form feed
\b Backspace
\a Alert (bell)
\e ESC character
\33 ESC in octal
\x7f DEL in hexadecimal
\cC Control-C
\x{263a} Unicode (smiley)

Perl provides us with some handy constructs that allow us to quote strings or individual words without having to worry (much) about escaping special characters. They are:

q/some literal string/; # does not interpolate
qq/some interpolated string/; # you guessed it - interpolations galore
qx/ls -la */; # executes the command between /  /

There are more, which I will explain when we reach Arrays and Regular expressions.

It’s interesting to note that these quoting constructs don’t necessarily have to delimit the quoted subject with / /. You can choose just about any character you’d like, as long as you follow the simple rule of using the matching closing character for any of [, {, or (. For all other characters, you just need to use the same one used to open the quotes.

q[some literal string];
q{another literal string};
q|one more literal string|;
q#I can even use pound characters!#;

Concatenation

Sometimes you may want to separate a string into two different pieces, one with each kind of quoting. You can do that with concatenations. Concatenations are represented by . (dot).

$some_string = 'This is a concatenated string' . "\n";

In the example above, we concatenate a single quoted-string with a newline within double quotes. Another form to concatenate strings is through the .= (concat and assign) operator. The example above could also be written like this:

$some_string = 'This is a concatenated string';
$some_string .= "\n";

Numbers

Like any other programming language, Perl also holds numbers. It can hold numbers in decimal, hexadecimal, binary, or octal notation.

$a_dec_number = 1;  # your every day number 1
$a_hex_number = 0x1; # number 1 in hex notation
$a_oct_number = 01; # number 1 in octal
$a_bin_number = 0b1; # number 1 in binary

Unlike most other languages, however, you can treat numbers as strings and get away with it.

$var1 = '1'; # you can use single quotes
$var2 = "2"; # or double quotes - Perl doesn't care
print $var1 + $var2; # prints out 3

Just like we have .= to concatenate strings, there are similar operators for numbers. They are:

Operator Function
+= Add and Assign
-= Subtract and Assign
/= Divide and Assign
*= Multiply and Assign

Example:

$i = 1; # $i is initiated with 1
$i += 5; # $i is now 6

Lastly, there are the auto-increment and auto-decrement operators: ++ and – –. They can be used either preceding or following the variable name.

$i++; # $i is now 7
$i--; # back to 6
++$i;# 7 again
--$i; # 6 once more

The difference between $i++ and ++$i is that the former returns the original value and then increments, while the latter first increments and then returns. The same goes for decrements. We can see this at work if we do a print:

print $i++ ; # prints 6
print $i; # prints 7 because of the previous increment
print ++$i; # prints 8, since it incremented before printing
print $i; # prints 8 again - no change after the previous increment

Empty or Undefined?

When you want variable to have no value, you can choose to either set it to an empty string (” or “”) or assign it the undef value.

$some_var = '';  # 2 single quotes with nothing inside
$variant = ""; # 2 double quotes with nothing inside
$other_var = undef; # using undef

We’ll see undef further along the course, and understand what role it plays in boolean functions.

« Variables and Data Structures | TOC | Arrays and Lists »

More To Explore

Documentation
vinny

bbPress Notify (No-Spam) Documentation

bbPress Notify (No-Spam) Documentation The bbPress Notify (No-Spam) is a free plugin that lets you control who gets notifications of new Topics and Replies in

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.