Perl has three main data types:
Strings can be concatenated by the dot (.) operator:$abc = "abc";
$def = "def";
where strings can be replaced by variables:$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";
$str = $abc . " " . "cde";# $str has the value 'abc cde'
A scalar variable can be defined by other scalar variables:$abc = "abc";
$cde = "cde";
$str = "$abc $cde";# $str has the value 'abc cde'
You could not write here$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 can assign a value to multiple variables:$bird_type = $bird; # $bird_type has now the value "penguin".
$observation = "I saw " . $no_of_birds . " " . $bird . "s.";# I saw two penguins.
and swap the values easily:($first, $second, $third, $fourth);
$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"; # 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
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");# the value of $result is 5
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 = index($str, "fo", (5 + 2) );# the value of $result is 40
With a start position:$str = "From forth the fatal loins of these two foes";
$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
So printing scalars to the console/terminal is really easy:print(expression) or print expression (without parentheses; I do not like this option);
$no = 1;
print($no);# prints the number 1
$no_1 = 1;
$no_2 = 2;
print($no_1);# prints the number 1
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 12 (without space!)
To print strings, including variables:$no_1 = 1;
$no_2 = 2;
print($no_1, " ", $no_2);# prints the number 1 2
What's happening here?$name = "Daniel";
$age = 15;
print("$name is $age years old.");# prints Daniel is 15 years old.
Notice the following: the scalar$bird = "penguin";
$no_of_birds = 2;
print("I saw " . $no_of_birds . " " . $bird . "s.");# I saw 2 penguins.
One could have similar results with thefor ($i = 0; $i < length("abcdefgh"); $i++) {
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
Easier -at least to remember-:print("2 + 2 = @{[2 + 2]}"); # 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"));
You could use nested functions.print(ucfirst("abc")); # prints Abc; the lowercase equivalent is: lcfirst
The string manipulation orchestra can be extended by regular expressions and modules (later more info on that).$str = "aBCD";
print(ucfirst (lc($str)));# prints Abcd
You'll get the same result when calling substr with four arguments:$str = "abcdefgh";
substr($str, 3, 3) = "-";
print($str);# prints abc-gh
Replacing the last three characters by *:$str = "abcdefgh";
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
The function 'chop' removes the last character of a string.$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
$str = "abc";
print(chop($str));# prints the removed character 'c'
print($str);# prints ab