The operator x in Perl

The operator x behaves differently depending on how it is used.

In scalar context it is used to create a line the desired length of repeated characters. We can say that this statement makes the "multiplication" of strings.

In list context (and if the left operand is written in parentheses or with using qw) he repeats an element a specified number of times.

Here is an example of operator x in scalar context:

▶ Run
#!/usr/bin/perl

my $str = 'A' x 4;

print $str;

The program will display a string consisting of four characters 'A': 'AAAA'.

If the left operand you specify multiple characters, they will all be repeated:

▶ Run
#!/usr/bin/perl

my $str = 'Abc' x 2;

print $str;

This example displays a string 'AbcAbc'.

Use an empty string

In scalar context, the operator x repeats the string that is specified in the left operand the number of times specified in the right operand.

If you specify as the left operand is the empty string '', in scalar context the result will always be an empty string, regardless of the value of the right operand:

▶ Run
#!/usr/bin/perl

use Data::Dumper;

my $str = '' x 100;

print Dumper $str;

At the conclusion of the program it will be seen that in $str contains an empty string.

There is no sense to use this operator in a scalar context, the empty string.

The value of the right operand

In scalar context, the right operand of the in operator x tells how many time you need to repeat the line that contains the left operand. In the case if the operand is a positive integer, then obviously, what happens as a result.

But numbers are not only positive.

  • If the right operand is the number 0, the result is always an empty string.
  • If the right operand is a negative number, the result is always an empty string.

In case the code is use warnings;, using a negative number as a right the operator will display a warning. Here is an example of a script that displays a string 'Negative repeat count does nothing at script.pl line 5.'.

▶ Run
#!/usr/bin/perl

use warnings;

my $str = 'a' x -3;

print $str;

If the right operand is a positive number with a fractional part, this fractional part is simply discarded. The result 'A' x 2.1 will be exactly the same as 'A' x 2.99 — line 'AA'.

List context

In list context, the operator x repeats the element. But besides that you need a list context, you need also to accommodate the left operand in parentheses. The code below will create an array @arr which contains 3 elements, each element is a string 'a':

▶ Run
#!/usr/bin/perl

use Data::Dumper;

my @arr = ('a') x 3;

print Dumper \@arr;

But if you do not specify the brackets in the left operand, and to write my @arr = 'a' x 3;, the array @arrwill be only one item, the string 'aaa'.

In addition to simple brackets, you can use the operator qw:

▶ Run
#!/usr/bin/perl

use Data::Dumper;

my @arr = qw(abc def) x 2;

print Dumper \@arr;

When you run this code in the array @arr will have 4 elements, string 'abc', 'def', 'abc', 'def'.

Official documentation

Here's a snippet of the output perldoc perlop about operator x:

Multiplicative Operators

...

    Binary "x" is the repetition operator. In scalar context or if the left
    operand is not enclosed in parentheses, it returns a string consisting of
    the left operand repeated the number of times specified by the right
    operand. In list context, if the left operand is enclosed in parentheses
    or is a list formed by "qw/STRING/", it repeats the list. If the right
    operand is zero or negative (raising a warning on negative), it returns an
    empty string or an empty list, depending on the context.

        print '-' x 80;             # print row of dashes

        print "\t" x ($tab/8), ' ' x ($tab%8);      # tab over

        @ones = (1) x 80;           # a list of 80 1's
        @ones = (5) x @ones;        # set all elements to 5

Other articles

Comments