Perl concat Function
last modified April 4, 2025
The Perl concat
function joins two or more strings together.
It's one of several ways to concatenate strings in Perl.
Unlike the dot operator (.
), concat
modifies the
first argument directly. It's efficient for building strings incrementally.
Basic concat Usage
The simplest way to use concat
is with two strings.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $str1 = "Hello"; my $str2 = " World"; concat($str1, $str2); print "$str1\n";
We demonstrate concat
joining two strings. The first string
is modified to contain the concatenated result.
$ ./basic.pl Hello World
Multiple Concatenations
concat
can join multiple strings in one operation.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $base = "Start"; concat($base, " Middle", " End", "!"); print "$base\n";
This script shows how concat
can append several strings at once.
All arguments after the first are appended to it.
$ ./multiple.pl Start Middle End!
concat vs Dot Operator
concat
differs from the dot operator in modifying behavior.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $str1 = "Hello"; my $str2 = " World"; my $dot_result = $str1 . $str2; concat($str1, $str2); print "Dot result: $dot_result\n"; print "concat result: $str1\n"; print "Original str1 modified: $str1\n";
The dot operator creates a new string, while concat
modifies
the first argument. This affects memory usage and performance.
$ ./compare.pl Dot result: Hello World concat result: Hello World Original str1 modified: Hello World
Building Strings Efficiently
concat
is useful for incremental string building.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $result = ""; my @parts = ("This", " is", " a", " complete", " sentence."); foreach my $part (@parts) { concat($result, $part); } print "$result\n";
We build a string efficiently by repeatedly concatenating parts. This avoids creating intermediate strings like the dot operator would.
$ ./building.pl This is a complete sentence.
concat with Variables and Literals
concat
works with both variables and string literals.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $name = "Alice"; my $greeting = "Hello"; concat($greeting, ", ", $name, "! How are you?"); print "$greeting\n";
This example mixes variables and literal strings in concatenation. The first argument must be a variable, others can be any strings.
$ ./mixed.pl Hello, Alice! How are you?
Performance Considerations
concat
can be faster than repeated dot operations.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; use Benchmark qw(cmpthese); my $iterations = 10_000; cmpthese($iterations, { 'dot' => sub { my $s = ""; $s = $s . "a" . "b" . "c"; }, 'concat' => sub { my $s = ""; concat($s, "a", "b", "c"); } });
This benchmark compares concatenation methods. concat
often
performs better for multiple operations by avoiding temporary copies.
$ ./performance.pl Rate dot concat dot 4762/s -- -25% concat 6349/s 33% --
concat in List Context
concat
can be used creatively with lists of strings.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my @words = ("Perl", " is", " powerful", " and", " flexible"); my $sentence = ""; concat($sentence, @words); print "$sentence\n";
We use concat
with an array to join all elements. The array
is flattened and all elements are appended to the first argument.
$ ./list.pl Perl is powerful and flexible
Best Practices
- Modify intentionally: Only use concat when you want to modify the first argument.
- Preallocate space: For large strings, preallocate size when possible.
- Consider readability: Sometimes the dot operator is clearer for simple cases.
- Watch scope: Be careful with concat in functions to avoid unexpected modifications.
Source
This tutorial covered Perl's concat
function with practical
examples demonstrating its usage and advantages in various scenarios.
Author
List all Perl tutorials.