Skip to content
VLSI Mentor

Perl Part I · Module 3 · Operators and Expressions

String Operators: . and x

Perl has exactly two operators dedicated to text: a dot that joins and an x that repeats. Both are small, and learning them takes a minute. What takes longer to absorb is the thing they demonstrate. Give the same two operands to the dot and to the plus sign and you get 104 from one and 14 from the other — not a different value but a different kind of answer. That is Chapter 2.6's point about what a Perl scalar is, arriving in a form you can run. It is also the groundwork for the most expensive confusion in the language, which Chapter 3.5 covers: a pair of equality operators that likewise differ only in which way they read their operands.

Foundation12 min readPerlPart IFundamentalsStringsOperators

Part I · Module 3 · Chapter 3.3 · String Operators: . and x

1. Two Operators

OperatorMeaningExampleResult
.concatenation — join two strings"alu" . "_tb"alu_tb
xrepetition — repeat a string"-" x 5-----

That is the whole family. Perl does most of its serious text work with functions and regular expressions, which Modules 4 and 11 cover; these two are the operators.

2. . Joins

Azvya Education Pvt. Ltd.VLSI Mentor
concat.pl
use strict;
use warnings;

my $name = "Ravi";

print "Hello, " . $name . "\n";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
Hello, Ravi

Note that you already had another way to do this. Chapter 2.5 showed interpolation:

Azvya Education Pvt. Ltd.VLSI Mentor
Two ways to the same line
print "Hello, " . $name . "\n";
print "Hello, $name\n";

Both produce Hello, Ravi. Which to prefer is a readability judgement rather than a rule:

  • Interpolation reads better when you are building a sentence around a few variables. "Hello, $name\n" is one quoted string that looks like its output.
  • Concatenation reads better when the pieces are expressions rather than plain variables, or when a piece comes from something that does not interpolate — a constant from Chapter 2.11, for instance, which is exactly why that chapter used the comma form.

What you should not do is mix them without reason. A line reading print "Hello, " . "$name" . "\n"; quotes a variable only to immediately unquote it, and a reader will stop to work out whether that was deliberate.

3. x Repeats

Azvya Education Pvt. Ltd.VLSI Mentor
repeat.pl
use strict;
use warnings;

print "-" x 20, "\n";
print "Report\n";
print "-" x 20, "\n";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
--------------------
Report
--------------------

The left operand is the string, the right is the count. Separators in a report are its most common use, and writing "-" x 20 instead of pasting twenty dashes means the number is visible, changeable, and countable by the reader without counting.

Two edge cases, both measured:

Azvya Education Pvt. Ltd.VLSI Mentor
repeat-edges.pl
use strict;
use warnings;

print "[", "ab" x 0,   "]\n";
print "[", "ab" x 2.7, "]\n";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
[]
[abab]
  • A count of zero gives the empty string. Not undef, not an error — nothing. That is convenient: a repetition driven by a computed count does not need a special case for zero.
  • A fractional count is truncated, not rounded. 2.7 gives two copies, not three. This is the same truncate-toward-zero behaviour you met in int() at Chapter 3.2 §2, and it is worth seeing the two as one rule rather than two facts: when Perl needs a whole number from a fraction and you have not asked for rounding, it discards the fraction.

4. The Operator Decides How the Operands Are Read

Here is the point of the chapter. The same two operands, given to two different operators:

Azvya Education Pvt. Ltd.VLSI Mentor
operator-decides.pl
use strict;
use warnings;

print 10 . 4, "\n";
print 10 + 4, "\n";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
104
14

Neither line is a trick and neither warns. 10 and 4 are the same two values in both. The dot wanted text, so it read them as the characters 1, 0 and 4 and joined them into 104. The plus wanted numbers, so it read them as quantities and added them to 14.

This is Chapter 2.6 stated as an experiment: a Perl scalar is not "a number" or "a string" with a label attached. The operator decides. A value participates as a number when a numeric operator touches it and as text when a textual one does, and the same stored value can do either.

Two consequences worth stating now, because both come back:

  • There is no error here to find. Perl did exactly what each operator asked. If 104 was not what you wanted, the bug is in your choice of operator, not in your data — and nothing in the output will tell you that.
  • Reversing it is just as quiet. "10" + "4" is 14, as Chapter 2.13 showed: text that looks numeric is read as a number without complaint.

Hold onto this. Chapter 3.5 is the same mechanism applied to equality, where it costs considerably more than a wrong-looking number.

5. Concatenating undef

One warning you will see while building strings:

Azvya Education Pvt. Ltd.VLSI Mentor
undef-concat.pl
use strict;
use warnings;

my $suffix;
my $name = "alu" . $suffix;

print "[$name]\n";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output — STDOUT
[alu]
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output — STDERR
Use of uninitialized value $suffix in concatenation (.) or string at undef-concat.pl line 5.

The result is alu — undef contributes the empty string — and the program continues. But the warning is the valuable part, and it is the same message Chapter 2.7 introduced. It is telling you that a value you expected to be there was not, and the reason that matters is that the output looks plausible: [alu] is a perfectly reasonable-looking name, and nothing in it says a piece went missing.

This is the clearest small case for why Chapter 1.11 insisted on use warnings in every file. Without it, this script silently produces a name built from incomplete data.

6. Why This Matters in Engineering Work

Building a path or an identifier. Script work in this trade is full of names assembled from parts: $block . "_" . $test . ".log". Every piece is a place where an undefined value can slip in and yield a shorter name that still looks like a name. alu_.log is a filename; it is also evidence that $test was empty, and only the warning says so.

Report formatting. "-" x $width and " " x $indent are how a plain-text report gets aligned columns, and keeping the width in a variable is what lets you change the layout in one place. This is the pattern Module 4 builds on.

The operator-decides rule is the one to internalise. The reason it earns a whole section in a chapter about two small operators is that it generalises. Every operator in this module reads its operands the way it wants, not the way the values were created. Once you expect that, == versus eq stops being a pair of arbitrary spellings and becomes an obvious consequence.

7. Common Mistakes

Using + to join text. "alu" + "_tb" is not alu_tb. The plus sign wants numbers, so it reads both operands numerically — they look like zero — and gives 0, with two warnings. The operator for joining is ..

Reaching for . when interpolation is clearer. "Hello, " . $name . "\n" works, and "Hello, $name\n" is easier to read. Use concatenation when the pieces are expressions, not when they are plain variables.

Expecting x to round its count. "ab" x 2.7 is abab. The fraction is discarded.

Getting the operands of x backwards. 20 x "-" is not twenty dashes. The string goes on the left.

Ignoring an uninitialized-value warning while building a string. The result still looks like a string, which is precisely why the warning matters.

8. Exercise

Exercise 1 — join three ways. Given my $block = "alu"; and my $test = "smoke";, produce the string alu_smoke.log three times: once with concatenation only, once with interpolation only, and once with a mix. Say which you would commit and why in one sentence.

Exercise 2 — the operator decides. Predict the output of each, then run all four: print 10 . 4;, print 10 + 4;, print "10" . "4";, print "10" + "4";. Record which lines produced warnings and explain the pattern in one sentence.

Exercise 3 — a ruled report. Write a script with my $width = 32; that prints a dashed line of that width, a title, and another dashed line. Then change $width to 48 and confirm one edit moved both rules.

Exercise 4 — find the missing piece. Write a script that builds my $log = $block . "_" . $test . ".log"; where $test is declared but never assigned. Record STDOUT and STDERR separately, state what $log ended up as, and say what a reader of the output alone would conclude.

Exercise 5 — the fractional count. Compute "=" x (10 / 3) and count the characters in the result. Then state the rule that predicted it, and name the function from Chapter 3.2 that behaves the same way.

9. Summary

  • . joins strings; x repeats one. Those are Perl's two string operators.
  • Interpolation is usually more readable than concatenation for plain variables; concatenation wins for expressions and for things that do not interpolate.
  • x with a count of zero gives the empty string, and a fractional count is truncated — "ab" x 2.7 is abab.
  • x also repeats lists, which is Chapter 3.10's subject.
  • The operator decides how its operands are read. 10 . 4 is 104 and 10 + 4 is 14, with no warning from either.
  • Concatenating undef yields the empty string and a warning — and output that looks plausible, which is why the warning is the useful part.

Chapter 3.4 covers the assignment operators, including the one that exists specifically to keep a real zero from being thrown away.

Where this fits

Part of the Perl curriculum.