Last Updated:

Concatenation and repetition operations | Perl

A binary concatenation operation, or join, combines two string operands into a single line. The sign of this operation is the period ".":

"one_string". "two_string"; # Result: "one_stringtwo_string" 

In the new line, the contents of the first operand and the contents of the second operand are joined without a space between them. Typically, this operation is used to assign a variable some new value. If you want to join two or more strings with spaces inserted between them, you should use the join operation (see Chapter 10, "Working with Strings"). You can, however, use the substitution property of a scalar variable in a string with a space insert (or any other character between them) to combine the value of a scalar variable into a string bounded by double quotation marks:

$sl = "one_string"; $s2 = "two_string"; $s = "$sl $s2"; # Value of $s: "one_string two_string" 

You can use a string concatenation operation sequentially in a single expression to connect multiple strings:

$sl = "one";
$s2 = "two";
$s3 = "three";
$s = $sl.$s2.$s3; # The value of $s is: "onetwothree"

The concatenation operation can be applied to both numeric literals and numeric data stored in scalar variables. The result is a string containing symbolic representations of two numbers:

$n1 = 23.5;
$n2 = Ze01;
$n = $nl.$n2; t Value of $n: "23.530"
$n = 23.5.3e01; # Value of $n:'"23.530"

Note that the last operator looks somewhat exotic and its semantics are not defined at first glance.

To work with strings in the Perl language, another operation is provided - the repetition of the line x (just a lowercase character "x"). This binary operation creates a new line in which the string specified by the left operand is repeated the number of times determined by the right operand:

"aA" x 2; # Result: "aaaa"
10.0 x "3"; # Result: "101010"
101e-1 x 3; # Result: "101010" $n = 010;
$n x 2; # Result: "88"
10.1 x 3.9; # Result: "10.110.110.1"
"101e-1" x 2; # Result: "101e-1101e-1"

Note that both numeric literals and variables containing numeric data can be used as the left operand. The right operand that specifies the number of repetitions can be any number or string that contains the correct decimal number.

This operation is useful if you want to print or display a repeating character or sequence of characters on the monitor screen. For example, the following statement will display a string consisting entirely of underscores on the monitor screen:

print "_" х 80;

The left operand of this operation can be a list enclosed in parentheses. In this case, the repeat operation x works as a repeater of the list, that is, its result will be a list in which the list of the left operand repeats the number of times specified by the right operand:

(1) x 3; # Result: (1, 1, 1) (1, 2) x 2; # Result: (1, 2, 1, 2) 

This is an example of using the Perl operation in different contexts: scalar and list (we will talk about contexts later in the same chapter). A repeat operation in a list context is useful for specifying an array of scalars with the same values of elements or groups of elements:

@array = ("a", "b") x 2; # Result: Oarray = ("a", "b", "a", "b")
  @array = ("a") x 3; # Result: @array = ("a", "a", "a")

Similarly, this operation can be used to initialize a hash array with the same values:

@keys = (one, two, three); # Defining hash array keys.
@hash{@keys} = ("a") x @keys; # Initialize hash array values.

In the last assignment operator on the right side, the scalar array @keys is used in a list context and presents a list of its values, whereas on the left side it is used in a scalar context and has a value equal to the number of its elements.

The sign of the repetition operation x ' should be separated by spaces from operands, as otherwise it can be perceived by the interpreter as referring to the lexeme, and not representing the repetition operation. For example, when parsing a string:

$nx$m;

... the interpreter will determine that it has two variables in a row $n x and $m, rather than a repeat operation on the contents of the $n variable, which will result in a syntax error.