Updated July 1, 2023
Introduction to Perl concatenate string
In Perl, you join or append the given string using the dot operator (.) for string concatenation. The dot operator (.) symbolizes the concatenation of strings. In general, string concatenation in Perl is simple because you use the string operator, such as the dot operator (.), to join two variables containing strings into a single string. This operation converts two different strings into one string.
Working of string concatenation in Perl with examples
One is concatenation and repetition, string operators. In Perl, string concatenation can be done using other ways also, such as using the dot operator (.), using the join() function, and using $ {var_name}. Now let us see the syntax for string concatenation with examples in the below section.
Syntax:
$res_str = $str1.$str2;
Parameter:
- res_str: this is a scalar variable that stores the concatenated string of the given two strings in the operands.
- str1: a scalar variable containing the string to be concatenated to another string stored in another variable.
- str2: this is also a scalar variable that contains the string to be concatenated to the first string stored in the previous variable.
This syntax in the above returns the combined string of two given operands with a string as a value.
Let us see a few examples of using the dot operator to concatenate a given string.
Examples
Let us discuss examples of Perl concatenate string
Example #1
Code:
#!/usr/bin/Perl
print "Demonstration of a concatenation of string using dot operator in Perl";
print "\n";
print "\n";
$str1 = "Educba ";
print "The first string to concatenate is given as follows:";
print "\n";
print $str1;
print "\n";
$str2 = "Institute";
print "The second string to concatenate with first is as follows:";
print "\n";
print $str2;
print "\n";
$res_str = $str1.$str2;
print "The resulting string after concatenating the above two string is as follows:";
print "\n";
print $res_str;
Output:
In the above program, we can see first we are declaring two different strings in two different variables such as “$str1” and “$str2” and in the above code, we are printing both the strings and then we are implementing the dot operator between two operands and storing the result in the variable “$res_str” where this dot operator returns the concatenated string which means it combines both the given string in “$str1” and “$str2” to one string and results in “$res_str.” The output is as seen in the above screenshot.
Example #2
Code:
#!/usr/bin/perl
print "Demonstration of concatenation of string using dot operator in Perl";
print "\n";
print "\n";
$str1 = "Python ";
print "The first string to concatenate is given as follows:";
print "\n";
print $str1;
print "\n";
$str2 = "Ruby ";
print "The second string to concatenate with first is as follows:";
print "\n";
print $str2;
print "\n";
$str3 = "Unix ";
print "The third string to concatenate with first is as follows:";
print "\n";
print $str3;
print "\n";
$str4 = "Perl ";
print "The fourth string to concatenate with first is as follows:";
print "\n";
print $str4;
print "\n";
$res_str = $str1.$str2.$str3.$str4;
print "The resulting string after concatenating the above two string is as follows:";
print "\n";
print $res_str;
Output:
Now we will see other ways of concatenating a string in the below section with an example
Example #3
Code:
#!/usr/bin/perl
print "Demonstration of concatenation of string using ${var_name} method in Perl";
print "\n";
print "\n";
$str1 = 'Welcome ';
print "The first string to concatenate is given as follows:";
print "\n";
print $str1;
print "\n";
$str2 = 'to Educba';
print "The second string to concatenate is given as follows:";
print "\n";
print $str2;
print "\n";
$res_str = "${str1}${str2}";
print "The concatenated string is as follows:";
print "\n";
print $res_str;
Output:
In the above program, we can see we have again declared two strings and also printed these strings, and then we have implemented the concatenation of these strings using the ${var_name} method to print the concatenated string of the given string, which results in a single string in the above as there is space at the end of first string “$str1” therefore in the resulting string, it will consider space as part of the string. Hence in the above screenshot, we can see the output of the concatenation of a given string into one string.
Conclusion
In this article, we reach the conclusion that string concatenation in Perl is achieved using string operators. In Perl, there are two primary string operators, one for concatenation and the other for repetition. Throughout the article, we explored three different approaches to string concatenation; other ways are using join() function and ${var_name}. This article showed examples of string concatenation using the dot operator and ${var_name} methods.
Recommended Articles
This is a guide to Perl concatenate strings. Here we discuss the Working of string concatenation in Perl and examples with code implementation. You may also have a look at the following articles to learn more –