Perl

Perl String Concatenation Examples

The string concatenation is mainly required to generate the formatted strings in any programming language. Two or more string values can be concatenated in different ways using the Perl script. The uses of the dot (.) operator, join() function, and variable expansion to concatenate multiple string values are shown in this tutorial.

Different String Concatenation Examples

Different ways of concatenating the strings in Perl are shown in this tutorial using multiple examples.

Example 1: Using the Dot (.) Operator

The most common way to concatenate the strings in Perl is the use of the dot (.) operator. The syntax of using this operator is given in the following:

Syntax:

The syntax of the dot(.) operator to concatenate the string is given as follows:

return_str = string1.string2..stringn

The concatenation result of two or more strings is returned into another string variable.

Create a Perl file with the following script that concatenates three string variables using the dot (.) operator. The concatenated string variable is printed later.

#!/usr/local/perl

use strict;
use warnings;

#Declare three string variables
my $string1 = 'Perl ';
my $string2 = 'String ';
my $string3 = 'Concatenation';

#Concatenate three strings
my $concanated_string = $string1.$string2.$string3;

#Print the concatenated string
print "The concatenated output of three strings is '$concanated_string'\n";

Output:

The following output appears after executing the script. The concatenated value is printed by the single quote(‘) here:

Example 2: Using the Shorthand Assignment Operator

Using the dot(.) shorthand operator is another way of concatenating the strings. The syntax to concatenate the strings by the shorthand operator is given in the following:

Syntax:

The syntax of the shorthand operator to concatenate the string is given as follows:

return_str .= other_string_val

Here, two string values are concatenated and the output is stored in the first string variable.

Create a Perl file with the following script that takes two string values from the user, concatenate these string values using a shorthand operator, and store the output in the first variable.

#!/usr/bin/perl

use strict;
use warnings;

print "Enter the first string value: ";
#Take first input
my $string1 = <>;
#Remove newline
chomp($string1);

print "Enter the second string value: ";
#Take second input
my $string2 = <>;
#Remove newline
chomp($string2);

#Concatenate the strings

$string1 .= " $string2";
#Print the concatenated string values
print "The concatenated string value is '$string1'\n";

Output:

The following output appears after executing the script. According to the output, “Perl” is taken as the first input and “Programming” is taken as the second input. The concatenated string value which is “Perl Programming” is printed in the output:

Example 3: Concatenate the String Values of an Array

The join() function is mainly used to concatenate the array values using a particular delimiter. The syntax of the join() function is given in the following:

Syntax:

The syntax of the join() operator to concatenate the string values of an array is given as follows:

return_str = join (delimiter,  array_of_string)

The first argument of the join() function is the delimiter that is used to concatenate the array values.

Create a Perl file with the following script that joins the values of the array with a delimiter and return the concatenated values as a string.

#!/usr/local/perl

use strict;
use warnings;
use Data::Dumper;

#Declare an array of string values
my @str_array = ("laptop", "mobile", "tablet", "headphone", "pen drive");

print "Array values are:\n";
#Display the array values through the dumper variable
print Dumper(\@str_array);

#Concatenate the array values join() function
my $concatenated_string = join(', ', @str_array);

#Print the concatenated array values
print "The concatenated string value is $concatenated_string\n";

Output:

The following output appears after executing the script. The array values are printed using the dump variable and the concatenate string of the array values is printed later:

Example 4: Using the Variable Expansion

The simplest way to concatenate multiple string values is to use the variable expansion. The syntax of concatenating the strings using the variable expansion is given in the following:

Syntax:

The syntax of the variable expansion to concatenate two or more string values is given as follows:

return_str = "$string1 $string2 … $stringN"

Here, the concatenated string value is returned into a variable.

Create a Perl file with the following script that concatenates multiple string values using the variable expansion. Two input values are taken from the user and the concatenated string of these values is printed later.

#!/usr/bin/perl

use strict;
use warnings;
use 5.34.0;

#Take the first input and remove the newline
say "Enter your first name:";
my $fname = <>;
chomp($fname);

#Take the second input and remove the newline
say "Enter your last name:";
my $lname = <>;
chomp($lname);

#Print the concatenated value of the firstname and lastname
say "Name : $fname $lname";

Output:

The following output appears after executing the script for the “Fahmida” and “Yesmin” input values:

Conclusion

Concatenating the strings in multiple ways using the Perl script is shown in this tutorial using multiple examples.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.