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.
$abc = "abc";Strings can be concatenated by the dot (.) operator:
$def = "def";
$str = "abc" . " " . "cde";where strings can be replaced by variables:# $str has the value 'abc cde'
$abc = "abc";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:
$str = $abc . " " . "cde";# $str has the value 'abc cde'
$abc = "abc";A scalar variable can be defined by other scalar variables:
$cde = "cde";
$str = "$abc $cde";# $str has the value 'abc cde'
$bird = "penguin";You could not write here
$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.
$bird_type = $bird;You can assign a value to multiple variables:# $bird_type has now the value "penguin".
$observation = "I saw " . $no_of_birds . " " . $bird . "s.";# I saw two penguins.
($first, $second, $third, $fourth);and swap the values easily:
$first = $second = 1;
$third = $fourth = 2;
($first, $third) = ($third, $first);# $first has the value 2 and $third the value 1.
$str = "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.# 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";
$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
$str = "From forth the fatal loins of these two foes";Find the index of the second occurrence of the substring 'fo': add simply as third argument a start position to start searching.
$result = index($str, "fo");# the value of $result is 5
$str = "From forth the fatal loins of these two foes";If a substring is not found, index returns -1. Use rindex to start searching at the end of a string.
$result = index($str, "fo", (5 + 2) );# the value of $result is 40
$str = "From forth the fatal loins of these two foes";With a start position:
$result = rindex($str, "fo");# the value of $result is 40
$str = "From forth the fatal loins of these two foes";
$result = rindex($str, "fo", (40 - 1));# the value of $result is 5
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;To print strings, including variables:
print($no);# prints the number 1
$name = "Daniel";
$age = 15;
print("$name is $age years old.");# prints Daniel is 15 years old.
for ($i = 0; $i < length("abcdefgh"); $i++) {One could have similar results with the 'split' function. However, some knowledge on arrays is therefore required: see chapter 4.
print(substr("abcdefgh", $i, 2));
print("\n");# prints ab cd ef gh on separate lines
}
print(length("abc"));# prints 3
print("abc" x 3);# prints abcabcabc
print("2 + 2 = @{[2 + 2]}");Easier -at least to remember-:# prints 2 + 2 = 4
print("2 + 2 = " . (2 + 2));# print("2 + 2 = " . 2 + 2); does not work!
print(scalar(reverse("abc")));
print(lc("ABC"));# prints abc; the uppercase equivalent is: uc
use utf8;
use open qw( :std :encoding(UTF-8) );
print(uc("Čerušňa"));
print(ucfirst("abc"));You could use nested functions.# prints Abc; the lowercase equivalent is: lcfirst
$str = "aBCD";The string manipulation orchestra can be extended by regular expressions and modules (later more info on that).
print(ucfirst (lc($str)));# prints Abcd
$str = "abcdefgh";You'll get the same result when calling substr with four arguments:
substr($str, 3, 3) = "-";
print($str);# prints abc-gh
$str = "abcdefgh";Replacing the last three characters:
substr($str, 3, 3, "-");# 3, 3 refers to three characters from the fourth character: 'def'
print($str);# prints abc-gh
$str = "abcdefgh";
substr($str, -3, 3, "*");
print($str);# prints abcde*
$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
$str = "abc";The function 'chop' removes the last character of a string.
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
$str = "abc";
print(chop($str));# prints the removed character 'c'
print($str);# prints ab