Perl Operators   «Prev  Next»
Lesson 5 String operators
ObjectiveWrite a program that creates a string and manipulates it using the qq and qw operators.

Perl String Operators

Just as the math operators work specifically with numbers, there are corresponding string operators for manipulating strings .
Operator Description
. Concatenation

Quote Operators in Perl

The single and double quotation marks are not the only way to quote strings. In fact, if your string has quotation marks in it, you can use a backslash to escape your quotation marks, like this:

$s = "This is a quote, \"The quick brown fox ...\"";

This process is prone to error, though. You can also use the qq operator to make your own quotes:
$s = qq{This is a quote, "The quick brown fox ..."};

You can use any character as the quotation delimiter, except
  1. any alphanumeric characters (0-9, a-z, or A-Z)
  2. underscores
  3. whitespace

If your delimiter is one of the four bracketing pairs,
  1. (round),
  2. <angle>,
  3. [square], or
  4. {curly} ,
you can use them in matched sets. Or, if you use another character, you must use the same character to start and end the quoted code.
For example, all of these are equivalent:
$s =qq{This is a quote, "The quick brown fox ..."};
$s = qq<This is a quote, "The quick brown fox ...">;
$s = qq/This is a quote, "The quick brown fox ..."/;

There are three different types of quote operators:
  1. qq for interpolated strings (like the double quotation marks)
  2. q for non-interpolated strings (like the single quotation marks)
  3. qw for making lists of words that are non-interpolated

The qw operator does not have a symbolic equivalent (like "" for qq or '' for q). For example, this code:
@a = qw(This is an array!);

is equivalent to this:
@a = ('This', 'is', 'an', 'array!');

The qw operator creates a list with one element for each of the words in the string. The whitespace in the string is used to delimit each of the words. But qw is the equivalent of single quotes, so there is no interpolation of Perl variables with qw.

q Single quotation mark
qq Double quotation mark
qw Word quote

Perl x-operator Repetition

The x operator (yes, that's the letter x) is used for repetition. It operates differently in scalar and list context, so let us look at each.

Scalar context

In scalar context, it will repeat the string in the left-hand term the number of times specified in the right-hand term. For example, this:
$x = 'a' x 25
will result in this:
aaaaaaaaaaaaaaaaaaaaaaaaa

Even if the left-hand side is a number, it will still be treated as a string. For example, this:
$x = 5 x 25

will result in this:
5555555555555555555555555


List context

In a list context, it will create a new list, repeating the list in the left-hand side the number of times specified in the right-hand side.
@x = ('a') x 25;

print join(':', @x)

The parenthesis are used to create a list context. The result is this:
a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a

In fact, the list in the left-hand side can have more than one element. For example, this:
@x = ('a',5) x 5; 
print join(':', @x)
will print this:
a:5:a:5:a:5:a:5:a:5


Here are some variants, most of which don't work:
$_ = "I have 2 numbers: 53147";
@pats = qw{
(.*)(\d*)
(.*)(\d+)
(.*?)(\d*)
(.*?)(\d+)
(.*)(\d+)$
(.*?)(\d+)$
(.*)\b(\d+)$
(.*\D)(\d+)$
};

for $pat (@pats) {
 printf "%-12s ", $pat;
 if ( /$pat/ ) {
  print "<$1> <$2>\n";
 }else {
   print "FAIL\n";
 }
}
That will print out:
(.*)(\d*) <I have 2 numbers: 53147> <>
(.*)(\d+) <I have 2 numbers: 5314> <7>
(.*?)(\d*) <> <>
(.*?)(\d+) <I have > <2>
(.*)(\d+)$ <I have 2 numbers: 5314> <7>
(.*?)(\d+)$ <I have 2 numbers:> <53147>
(.*)\b(\d+)$ <I have 2 numbers:> <53147>
(.*\D)(\d+)$ <I have 2 numbers:> <53147>

String Concatenation Operator in Perl

The concatenation operator is used to create one string out of two:
$s1 = 'This is string one.'; 
$s2 = 'This is string two.';
$s = $s1 . $s2;

$s is now:
'This is string one.This is string two.'

Perl String - Exercise

Click the Exercise link belows to write a program in which you will use the string operators we have just examined to manipulate a string and array.
Perl String - Exercise

Advanced Perl Programming