String operators#

Two operators in this family — concatenation and repetition. They play the same role for strings that + and * play for numbers.

Operator

Operation

Arity

Associativity

Context

.

concatenation

binary

left

string

x

repetition

binary

left

mixed

Both coerce their operands to string context (with the usual numeric-to-string conversion: 42 becomes "42", 1.5 becomes "1.5", etc.).

. — concatenation#

Joins the two operands. The result is the concatenation of the left and right strings:

"Hello" . " " . "World"        # "Hello World"
"x = " . 42                    # "x = 42"            (numeric coercion)
"err: " . $!                   # "err: <strerror>"

Compound .= appends in place:

my $s = "a";
$s .= "b";       # "ab"
$s .= "c";       # "abc"

.= is faster than $s = $s . "..." for repeated appending — not by an asymptotic margin in modern Perl, but enough to matter in tight loops on long strings. pperl’s runtime mutates the internal buffer in place when the string’s reference count is 1 (see PetaPerl-specific notes below).

For large numbers of pieces, join is generally clearer than a chain of .:

join(", ", @items)
"hello, world, !"              # hand-built; readable for small arity

join(", ", $a, $b, $c, $d)     # cleaner than $a.", ".$b.", ".$c.", ".$d

x — repetition#

Repeats its left operand a number of times given by the right operand. Behaviour depends on the context of the left operand:

  • Scalar context on the left → string repetition.

  • List context on the left (parens around the left operand) → list repetition.

"ab" x 3              # "ababab"          (string)
"-" x 40              # 40-character ruler

(0) x 5               # (0, 0, 0, 0, 0)   (list)
(1, 2) x 3            # (1, 2, 1, 2, 1, 2)

The parens-on-the-left distinction trips beginners. Without parens, the left side is in scalar context regardless of how it looks:

1, 2 x 3              # (1, "222")        -- Whoops.
                      # x binds tighter than , so this is
                      # (1, (2 x 3)) — and 2 in scalar context
                      # stringifies, so 2 x 3 is "222".

(1, 2) x 3            # (1, 2, 1, 2, 1, 2)   -- list repetition

Compound x= replicates in place:

my $line = "=";
$line x= 40;          # "========================================"

A non-positive count returns the empty string (or empty list for list context), without warning:

"abc" x  0            # ""
"abc" x -3            # ""
("a", "b") x 0        # ()

A non-integer count is truncated toward zero:

"x" x 3.7             # "xxx"      (3.7 truncates to 3)
"x" x -0.5            # ""

Numeric vs string operators#

Mixing numeric and string operators on the same operand produces predictable but sometimes surprising results, because each operator imposes its own context:

"5" + "3"             # 8        (+ wants numeric → both coerce to numbers)
 5  .  3              # "53"     (. wants string  → both coerce to strings)
"5" * "3"             # 15
 5  x  3              # "555"    (x wants string left, number right)

There is no operator that «guesses» — the operator picks the context, full stop.

See also#