Strings
Single and double quotes
Perl has two kinds of string literal with one important difference. Single quotes take the text as written: no variables, no escape sequences except a backslash before a quote or a backslash. Double quotes interpolate variables and understand escapes such as \n and \t.
Use single quotes when the text is data, double quotes when the text is a template.
my $name = "Ada"; print 'hello $name\n'; # prints hello $name\n print "hello $name\n"; # prints hello Ada
perl demo.pl
hello $name\nhello Ada
The letters q and qq are the same two literals without the quote characters, which is handy when the text itself is full of quotes. The character after the letter becomes the delimiter.
my $name = "Ada"; print q{it's fine}; print qq{say "$name"\n};
perl demo.pl
it's finesay "Ada"
Concatenation and repetition
The dot joins strings and the letter x repeats them. Both have an assignment form that changes the left side in place.
my $greeting = "hello" . " world"; print "$greeting\n"; # hello world $greeting .= "!"; print "-" x 20, "\n"; # --------------------
perl demo.pl
hello world
--------------------
length, substr, index
Three functions cover most string surgery. length counts characters, substr cuts a piece out, and index finds the position of a smaller string.
my $path = "/var/log/app.log"; print length $path, "\n"; # 16 print substr($path, 0, 4), "\n"; # /var print index($path, "log"), "\n"; # 5
perl demo.pl
16
/var
5
substr also assigns: give it a replacement as the fourth argument and the original string changes. Negative offsets count from the end, so substr $path, -3 returns the extension.
my $path = "/var/log/app.log"; print substr($path, -3), "\n"; # log substr($path, -3) = "txt"; print "$path\n"; # /var/log/app.txt
perl demo.pl
log
/var/log/app.txt
Case and chomp
Input from a file or a terminal almost always ends with a newline that you do not want. chomp removes exactly one trailing newline; chop removes the last character whatever it is, which is rarely what you mean.
Case conversion comes in three flavors, each returning a new string rather than changing the original.
my $line = " Hello World\n"; chomp $line; print "[", uc $line, "]\n"; # [ HELLO WORLD] print "[", lc $line, "]\n"; # [ hello world] print "[", ucfirst lc $line, "]\n"; # [ hello world]
perl demo.pl
[ HELLO WORLD]
[ hello world]
[ hello world]
Leading spaces survive all of them, and the brackets show where the text begins. Use a regular expression from lesson 5.1 when you need to trim both ends.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is the difference between
'hello $name\n'and"hello $name\n"?Answer
The single-quoted one keeps
$nameand the two characters\nas written, the double-quoted one interpolates the variable and the newlineSingle quotes take the text literally, apart from
'and\; double quotes interpolate variables and understand escapes. -
What are
q{...}andqq{...}?Answer
The single-quoted and double-quoted literals written with a delimiter of your choice
The letter after
qorqqbecomes the delimiter, which is handy when the text itself holds quotes. -
Which operators join two strings and repeat one?
Answer
.andxThe dot concatenates and
xrepeats;joinis a function for lists and+adds numbers. -
What is the difference between
chompandchop?Answer
chompremoves one trailing newline,chopremoves the last character whatever it ischompis the safe one for input lines;chopcan eat a character you wanted. -
What does
substr($path, -3)return when$pathis/var/log/app.log?Answer
logA negative offset counts from the end of the string, so the last three characters come back.
-
Which call returns a new string with the first letter in upper case?
Answer
ucfirst $sucfirstchanges the first character only,ucchanges the whole string, andlcfirstlowers the first. -
What does this program print?
my $name = "Ada"; print 'hello $name\n';Answer
hello $name\n
Single quotes keep the dollar sign and the backslash-n as two literal characters, so no interpolation happens.
-
What does this program print?
my $s = "abc" . "def"; print length $s, "\n";Answer
6
The dot builds one string of six characters, and
lengthcounts them. -
What does this program print?
my $path = "/var/log/app.log"; print substr($path, -3), "\n";Answer
log
The negative offset starts three characters from the end and runs to it, which is the extension without the dot.
-
What does this program print?
my $line = "Hello\n"; chomp $line; print length $line, "\n";Answer
5
The string has five letters and a newline;
chompremoves the newline, so the length drops from 6 to 5.
Article author: Arthur Isaev