68

In Python, if I do this:

print "4" * 4

I get

> "4444"

In Perl, I'd get

> 16

Is there an easy way to do the former in Perl?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
izb
  • 50,101
  • 39
  • 117
  • 168
  • This is also possible with string variables (and string expressions), see [my answer](http://stackoverflow.com/a/30100928/2932052). – Wolf May 07 '15 at 12:20

6 Answers6

112
$ perl -e 'print "4" x 4; print "\n"'
4444

The x operator is documented in perldoc perlop. Here binary means an operator taking two arguments, not composed of bits, by the way.

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, 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

perl -e is meant to execute Perl code from the command line:

$ perl --help
Usage: perl [switches] [--] [programfile] [arguments]
  
  -e program     one line of program (several -e's allowed, omit programfile)
brian d foy
  • 129,424
  • 31
  • 207
  • 592
Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
  • 1
    Just a remark - it may not be clear to all perl users (especially the new ones) what the -e option does, so it would be better to provide direct code example (as Paul's response does). – Grey Panther Nov 10 '08 at 10:13
  • `perl -Esay+4x4` Here, '-E' enables 'use 5.010' (particularly - the 'say' feature). `say "$var"` is the same as `print "$var\n"`. In scalar context the 'x' operator always returns a string, so there is no need to use quotes here. – jfs Nov 10 '08 at 12:53
  • Now that would be confusing :-) – Vinko Vrsalovic Nov 10 '08 at 14:45
  • And no reason to use double quotes, to pile on the picky. – Axeman Nov 10 '08 at 16:14
  • I would like to see the reason, for the differences, to be cited in this answer. The reason is that, in Perl you can treat a string as a number, and it will automatically become a number. – Brad Gilbert Nov 10 '08 at 17:13
  • 6
    I have a reason to use quotes, readability and intent. The fact you can omit them doesn't mean you have to (or even should). – Vinko Vrsalovic Nov 10 '08 at 19:19
45

In Perl, you want to use the "x" operator.

Note the difference between

"4" x 4

and

("4") x 4

The former produces a repeated string:

"4444"

the latter a repeated list:

("4", "4", "4", "4")
bart
  • 7,640
  • 3
  • 33
  • 40
  • 5
    In Perl 6 the `x` operator always returns string (left operand is evaluated in a string context e.g., 4x4 -> "4"x4 -> "4444"), `xx` - a repeat op for lists e.g., 4xx4 -> (4)xx4 -> (4,4,4,4). – jfs Nov 10 '08 at 13:19
  • I cannot confirm that for Perl v5.32 – Wolf Feb 18 '22 at 17:26
17

It's very similar in Perl

print "4" x 4;
fedorqui
  • 275,237
  • 103
  • 548
  • 598
The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
12

FWIW, it’s also print 4 x 4 in Perl.

In general, in Perl, operators are monomorphic, ie. you have different sets of operators for string semantics, for numeric semantics, for bitwise semantics, etc., where it makes sense, and the type of the operands largely doesn’t matter. When you apply a numeric operator to a string, the string is converted to a number first and you get the operation you asked for (eg. multiplication), and when you apply a string operator to a number, it’s turned into a string and you get the operation you asked for (eg. repetition). Perl pays attention to the operator first and the types of the operands only second – if indeed it pays them any mind at all.

This is the opposite of Python and most other languages, where you use one set of operators, and the types of the operands determine which semantics you’ll actually get – ie. operators are polymorphic.

Aristotle Pagaltzis
  • 112,955
  • 23
  • 98
  • 97
  • 1
    The fact you can doesn't mean you should, necessarily. I think that making intentions clear makes the code more readable for everybody. Omitting quotes makes it less readable to my eyes. – Vinko Vrsalovic Nov 10 '08 at 14:57
  • 2
    a) Did you see anything in my answer about what you should or should not do? (This `4 x 4` is unlikely to show up in real Perl code verbatim anyway.) b) If it is less readable to you, you are paying attention to the wrong things (the forms of the operands, rather than the operator). – Aristotle Pagaltzis Nov 11 '08 at 02:12
  • 2
    a) Did you see anything in my comment about whether you said what one should or should not do? b) When you deal with many languages on almost a daily basis, every little bit helps. Using quotes is totally clear an unambiguous in (almost) any language, omitting them means one extra mental step to take. – Vinko Vrsalovic Jul 21 '09 at 05:25
2

All answers, given so far, missed mentioning that the operator x does not only work on string literals, but also on variables that are strings or expressions that evaluate to strings like

use feature 'say';

my $msg = "hello ";
say $msg x 2;
say chr(33) x 3;

like this

hello hello
!!!

and, even more important, x does an automatic conversion of expressions into strings if they aren't already (thanks to ggorlen for pointing me into that direction!). So for example

say 4 x 2;
say [$msg] x 2;

will result in something like the following as output

44
ARRAY(0x30ca10)ARRAY(0x30ca10)
Wolf
  • 9,679
  • 7
  • 62
  • 108
2

If you want to print 10 character "A"s, you can also do this

perl -e 'print "A" x 10'; echo

Example with output

user@linux:~$ perl -e 'print "A" x 10'; echo
AAAAAAAAAA
user@linux:~$ 
Charlotte Russell
  • 1,355
  • 1
  • 13
  • 16