ZetCode

Perl concat - String Concatenation in Perl

last modified October 8, 2026

Perl has no built-in concat function. Strings are concatenated with the concatenation operator . and the compound assignment operator .=. The join function combines a list of strings with a separator.

This tutorial shows these operators and functions, and how to write your own concat subroutine if you want that name.

The concatenation operator

The dot operator creates a new string from its two operands. The operands themselves are not changed.

basic.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $str1 = "Hello";
my $str2 = " World";

my $result = $str1 . $str2;
say $result;
say "$str1 is unchanged";

We join two strings with the dot operator and store the result in a new variable. The original strings remain the same.

$ ./basic.pl
Hello World
Hello is unchanged

Appending with .=

The .= operator appends the right operand to the variable on the left. The variable is modified in place.

append.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $text = "Perl";
$text .= " is";
$text .= " powerful";

say $text;

Each .= adds one piece to the end of $text.

$ ./append.pl
Perl is powerful

Joining lists with join

join takes a separator as its first argument, followed by a list of strings. The separator is placed between the items, not at the beginning or the end.

join.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my @words = ("Perl", "is", "powerful");

say join(" ", @words);
say join("-", 2026, 10, 8);

The first example joins the words with a space. The second joins three numbers with a hyphen.

$ ./join.pl
Perl is powerful
2026-10-8

Concatenation and interpolation

Variables can be concatenated with the dot operator, or interpolated inside a double-quoted string. Both approaches produce the same result.

interpolation.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $name = "Alice";
my $age = 30;

my $s1 = "Name: " . $name . ", age: " . $age;
my $s2 = "Name: $name, age: $age";

say $s1;
say $s2;
say $s1 eq $s2 ? "Same" : "Different";

The comparison at the end confirms that both strings are identical. Interpolation is often easier to read when the string contains several variables.

$ ./interpolation.pl
Name: Alice, age: 30
Name: Alice, age: 30
Same

Numbers and concatenation

The dot operator treats its operands as strings. Numbers are converted to text before they are joined. The + operator, in contrast, adds numbers.

numbers.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $n = 42;

say "Count: " . $n;
say 3 . 4;
say 3 + 4;

3 . 4 produces the string "34", while 3 + 4 produces the number 7.

$ ./numbers.pl
Count: 42
34
7

Building a string in a loop

.= is the usual way to build a string piece by piece inside a loop.

building.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $report = "";

foreach my $i (1 .. 3) {
    $report .= "Line $i\n";
}

print $report;

Each iteration appends a new line to $report. The string is printed once at the end.

$ ./building.pl
Line 1
Line 2
Line 3

Writing a concat helper

Perl does not provide concat, but a subroutine with this name can be defined in your own code. The following one joins all of its arguments with an empty separator.

own_concat.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

sub concat {
    return join '', @_;
}

say concat("Hello", ", ", "World", "!");

The subroutine returns the joined string. Calling it does not modify any of its arguments.

$ ./own_concat.pl
Hello, World!

Best Practices

Source

Perl operators documentation

This tutorial covered string concatenation in Perl using the dot operator, .=, and join, with a note on the missing concat function.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Perl tutorials.