String operator

The concatenation operator . is used to add strings together:

print 'abc' . 'def';        # Prints abcdef
print $a . $b;              # Concatenates the string values of $a and $b

Binary x is the string repetition operator. In scalar context, it returns a concatenated string consisting of the left operand repeated the number of times specified by the right operand:

print '-' x 80;                           # Prints row of dashes
print "\t" x ($tab/8), ' ' x ($tab%8);    # Tabs over

In list context, if the left operand is a list in parentheses, the x works as a list replicator rather than a string replicator. This is useful for initializing all the elements of an array of indeterminate length to the same value:

@ones = (1) x 80;           # A list of 80 1s
@ones = (5) x @ones;        # Set all elements to 5

Get Perl in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.