1. Scalar

Perl has three main data types: $scalar, @array (chapter 4), and %hash (chapter 1, part 2). A Scalar represents a Single value. A scalar variable - a name preceded by the '$' sigil like the character 'S' - contains a single value. A string or a number are examples of a single value or a scalar.

1.1 Create

The declaration of a scalar:
$abc = "abc";
$def = "def";
Strings can be concatenated by the dot (.) operator:
$str = "abc" . " " . "cde"; # $str has the value 'abc cde'
where strings can be replaced by variables:
$abc = "abc";
$str = $abc . " " . "cde"; # $str has the value 'abc cde'
Concatenation is often useful for readability. However, it is not always necessary. Variable interpolation ( = replacing a variable with the value of that variable) can give correct results:
$abc = "abc";
$cde = "cde";
$str = "$abc $cde"; # $str has the value 'abc cde'
A scalar variable can be defined by other scalar variables:
$bird = "penguin";
$no_of_birds = "two";
$bird_type = $bird; # $bird_type has now the value "penguin".
$observation = "I saw $no_of_birds ${bird}s."; # I saw two penguins.
You could not write here $birds, because the variable $birds does not exist. Hence curly braces ${bird}s are necessary in order to separate the character 's' from the variable name. The last example can be done with concatenation using the dot operator.
$bird_type = $bird; # $bird_type has now the value "penguin".
$observation = "I saw " . $no_of_birds . " " . $bird . "s."; # I saw two penguins.
You can assign a value to multiple variables:
($first, $second, $third, $fourth);
$first = $second = 1;
$third = $fourth = 2;
and swap the values easily:
($first, $third) = ($third, $first); # $first has the value 2 and $third the value 1.
1.1.1 Short cuts q and qq
Four ways to get the same result...I prefer the third option.
$str = "Did you say \"Hello?\"\n"; # backslash necessary
$str = qq(Did you say "Hello?"\n);
$str = 'Did you say "Hello?"' . "\n";# my preferred option
$str = q(Did you say "Hello?") . "\n";
qq for double apostrophes (interpolation is possible) and q for a single one (for literally output). You can use pairs of delimiters of your choice like //, [], {} etc.

1.2 Access

For now, we can discern access tools for strings and numbers. In this chapter, we only discuss the (sub)string tools.
1.2.1 Extract a substring
$result = substr("abcdefgh", 2); # the value of $result is cdefgh; strings are zero-based, i.e. the first character has the index 0
$result = substr("abcdefgh", 2, 3); # the value of $result is cde
1.2.2 Find index of a substring (aka 'check if a string contains a substring')
Find the index of the first occurrence of the substring 'fo'.
$str = "From forth the fatal loins of these two foes";
$result = index($str, "fo"); # the value of $result is 5
Find the index of the second occurrence of the substring 'fo': add simply as third argument a start position to start searching.
$str = "From forth the fatal loins of these two foes";
$result = index($str, "fo", (5 + 2) ); # the value of $result is 40
If a substring is not found, index returns -1. Use rindex to start searching at the end of a string.
$str = "From forth the fatal loins of these two foes";
$result = rindex($str, "fo"); # the value of $result is 40
With a start position:
$str = "From forth the fatal loins of these two foes";
$result = rindex($str, "fo", (40 - 1)); # the value of $result is 5

1.3 Print

The syntax of 'print':
print(expression) or print expression (without parentheses; I do not like this option);
So printing scalars to the console/terminal is really easy:
$no = 1;
print($no); # prints the number 1
$no_1 = 1;
$no_2 = 2;
print($no_1); # prints the number 1
$no_1 = 1;
$no_2 = 2;
print($no_1, $no_2); # prints the number 12 (without space!)
To print the last result with a space, use a 'space' element:
$no_1 = 1;
$no_2 = 2;
print($no_1, " ", $no_2); # prints the number 1 2
To print strings, including variables:
$name = "Daniel";
$age = 15;
print("$name is $age years old."); # prints Daniel is 15 years old.
What's happening here?
$bird = "penguin";
$no_of_birds = 2;
print("I saw " . $no_of_birds . " " . $bird . "s."); # I saw 2 penguins.
Notice the following: the scalar $no_of_birds was defined as a number: $no_of_birds = 2. However, despite of this, the output is: 'I saw 2 penguins'. In other words, the dot operator evaluates the number 2 in string context!

1.4 Iterate

The built-in function 'substr' can be used when looping (see chapter 7) through a string:
for ($i = 0; $i < length("abcdefgh"); $i++) {
print(substr("abcdefgh", $i, 2));
print("\n"); # prints ab cd ef gh on separate lines
}
One could have similar results with the 'split' function. However, some knowledge on arrays is therefore required: see chapter 4.

1.5 Operate

1.5.1 String tools

String-handling tools are an important part of a programming language. Here some tools, you've to know.
1.5.1.1 String length
print(length("abc")); # prints 3
1.5.1.2 String repetition
print("abc" x 3); # prints abcabcabc
1.5.1.3 Evaluate expressions within a string
print("2 + 2 = @{[2 + 2]}"); # prints 2 + 2 = 4
Easier -at least to remember-:
print("2 + 2 = " . (2 + 2));# print("2 + 2 = " . 2 + 2); does not work!
1.5.1.4 Reverse a string
print(scalar(reverse("abc")));
1.5.1.5 Lowercase a string
print(lc("ABC")); # prints abc; the uppercase equivalent is: uc
1.5.1.6 Uppercase a UTF8-string
use utf8;
use open qw( :std :encoding(UTF-8) );
print(uc("Čerušňa"));
1.5.1.7 Uppercase/lowercase the first character of a string
print(ucfirst("abc")); # prints Abc; the lowercase equivalent is: lcfirst
You could use nested functions.
1.5.1.8 Lowercase a string and uppercase its first character
$str = "aBCD";
print(ucfirst (lc($str))); # prints Abcd
The string manipulation orchestra can be extended by regular expressions and modules (later more info on that).

1.5.2 Substring tools

Substring manipulation is also an important part of a programming language. Here some tools, you've to know.
1.5.2.1 Replace a substring
$str = "abcdefgh";
substr($str, 3, 3) = "-";
print($str); # prints abc-gh
You'll get the same result when calling substr with four arguments:
$str = "abcdefgh";
substr($str, 3, 3, "-");# 3, 3 refers to three characters from the fourth character: 'def'
print($str); # prints abc-gh
Replacing the last three characters by *:
$str = "abcdefgh";
substr($str, -3, 3, "*");
print($str); # prints abcde*
1.5.2.2 Insert a substring
To insert characters into a string, you can use 'substr' also.
$str = "abc";
print(substr($str, 0, 0, "X")); # prints Xabc
$str = "abc";
print(substr($str, 3, 0, "X")); # prints abcX
$str = "abc";
print(substr($str, 2, 0, "X")); # prints abXc
1.5.2.3 Delete a substring
To delete characters from a string, you can use 'substr' again.
$str = "abc";
print(substr($str, 0, 1, "")); # prints bc
$str = "abc";
print(substr($str, 0, 2, "")); # prints c
$str = "abc";
print(substr($str, 2, 1, "")); # prints ab
The function 'chop' removes the last character of a string.
$str = "abc";
print(chop($str)); # prints the removed character 'c'
print($str); # prints ab

Exit: scalar context sensitivity

If the left side (the variable being assigned a value) is a scalar variable, the right side (the values being assigned) is evaluated in scalar context. Examples of this feature will presented later: we've to look first at other data types. For now, recall 'context sensitivity' when programming Perl.