Perl string concatenation - How to concatenate strings with Perl

Perl string FAQ: How do I concatenate Perl strings?

When you work with Perl, you're often working with strings, and very often you need to concatenate strings. For instance, I recently had a need to create a temporary filename, and wanted to use the original filename as part of the temporary name.

I was going to get the first part of the filename (the filename prefix) by calling a function that generated temporary filenames. After getting the first part of the filename, I wanted to prepend the directory "/tmp/" to the beginning of the name, and append the filename extension ".tmp" to the end of the name. Here's how I did it.

Perl string concatenation - Method #1 - Using ${name}

One of the best and simplest ways to concatenate Perl strings is with this syntax:

$name = 'foo';
$filename = "/tmp/${name}.tmp";

In this example, I have a Perl string variable named $name that contains the text "foo", and I use this Perl syntax to embed the value of that variable in the string that I'm assigning to the new variable $filename. This example prepends the string '/tmp/' to the variable $name, and appends the '.tmp' filename extension to $name.

These days I use this Perl string concatenation approach more than any other.

Perl string concatenation - Method #2 - using Perl's dot operator

Another way to concatenate Perl strings is to use the "dot" operator (i.e., the decimal character). In some circumstances it can make your Perl script easier to read than the syntax shown above.

To concatenate Perl strings this way, just place the dot operator in between the strings you want to merge on the right side of the '=' symbol in your statement, as shown here:

$name = checkbook'; 
$filename = '/tmp/' . $name . '.tmp'; 

# $filename now contains "/tmp/checkbook.tmp"

Again, this example creates a Perl variable named $filename that contains the concatenated string contents shown on the right side of that statement.

Perl string concatenation - Method #3 - using Perl's join function

Another way to create the desired variable $filename is to use the Perl join function. With the join function you can merge as many Perl strings together as you like, and you can specify what token you want to use to separate each string.

The code shown in Listing 2 below uses the Perl join function to achieve the same result as the code shown in Listing 1:

$name = 'checkbook'; 
$filename = join '', '/tmp/', $name, '.tmp'; 

# $filename now contains "/tmp/checkbook.tmp"

I rarely use the Perl join function to merge simple strings like this, but I thought I'd show this as another possible way to concatenate Perl strings.

Perl string concatenation - Summary

I hope these Perl string concatenation examples have been helpful. I just updated this article in 2011 to update it to the current Perl "best practices".

Good point, @Dave Cross, about the single-/double-quotes. Ruby is like that, too. Also, the interpolation with "Hello, #{'world'}" is another Ruby-ism. Now that I'm learning Perl, I have more of a fondness for Ruby! Cheers.


As you suggested, I did the following comparison of the different ways to concatenate strings : concatenate 1 character x 1 million times.

sub DateHMS
# in : nothing
# out : date format: 10:26:42
$DH=`date '+%H:%M:%S'`;
return $DH;

print "dot: start = ".$datehms;
for ($i=1; $i<=$max; $i++)
{ $str = $str . '1';
print "\nincluded : start = ".$datehms;
for ($i=1; $i<=$max; $i++)
{ $str = "${str}1";
print "\noperator : start = ".$datehms;
for ($i=1; $i<=$max; $i++)
{ $str .= '1';
print "\njoin : start = ".$datehms;
for ($i=1; $i<=$max; $i++)
{ $str = join "", $str, "1";
print "\nend = ".$datehms."\n";

I ran it onto a Fujitsu Primepower 450 (4 sparc64 cpu x 1.648 Mhz). The results are very similar but for one method :
dot: start = 11:06:31
included : start = 11:12:12
operator : start = 11:18:01
join : start = 11:18:02
end = 11:23:52

that means :
dot : 5 mn 41 s
included : 5 mn 49 s
join : 5 mn 49 s

and operator: less than 1 second !!!!!
I ran it with 10 000 000 times : 9 seconds.

What method will you choose then ?

I'm interested in results on others kinds of architectures.



I would just like to confirm Frangi's observations.

Was working with a several large files ~ 1 Gig each and appending various pieces together per line. Switching from $x = $x."\t$y"; to $x .= "\t$y"; took the run time from 7 hours to 15min.

Thought I'd pass it along.


Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.