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.

  1. What is the difference between 'hello $name\n' and "hello $name\n"?

    Answer

    The single-quoted one keeps $name and the two characters \n as written, the double-quoted one interpolates the variable and the newline

    Single quotes take the text literally, apart from ' and \; double quotes interpolate variables and understand escapes.

  2. What are q{...} and qq{...}?

    Answer

    The single-quoted and double-quoted literals written with a delimiter of your choice

    The letter after q or qq becomes the delimiter, which is handy when the text itself holds quotes.

  3. Which operators join two strings and repeat one?

    Answer

    . and x

    The dot concatenates and x repeats; join is a function for lists and + adds numbers.

  4. What is the difference between chomp and chop?

    Answer

    chomp removes one trailing newline, chop removes the last character whatever it is

    chomp is the safe one for input lines; chop can eat a character you wanted.

  5. What does substr($path, -3) return when $path is /var/log/app.log?

    Answer

    log

    A negative offset counts from the end of the string, so the last three characters come back.

  6. Which call returns a new string with the first letter in upper case?

    Answer

    ucfirst $s

    ucfirst changes the first character only, uc changes the whole string, and lcfirst lowers the first.

  7. 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.

  8. What does this program print? my $s = "abc" . "def"; print length $s, "\n";

    Answer

    6

    The dot builds one string of six characters, and length counts them.

  9. 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.

  10. 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; chomp removes the newline, so the length drops from 6 to 5.

Article author: Arthur Isaev