Operators are the foundation of any programming language. Thus, the functionality of Perl programming language is incomplete without the use of operators. A user can define operators as symbols that help to perform specific mathematical and logical computations on operands. String are scalar variables and start with ($) sign in Perl. The String is defined by user within a single quote (‘) or double quote (“) . There are different types of string operators in Perl, as follows:
- Concatenation Operator (.)
- Repetition Operator (x)
- Auto-increment Operator (++)
Concatenation Operator(.)
Perl strings are concatenated with a Dot(.) symbol. The Dot(.) sign is used instead of (+) sign in Perl. This operator takes two scalars variables as operands and combines them in a single scalar variable. Both scalars i.e left and right will convert into a single string.
Example:
# Perl program to demonstrate the # Concatenation Operator(.) in String #!/usr/bin/perl # Input first string $first_string = "Geeks"; # Input second string $second_string = "forGeeks"; # Implement Concatination operator(.) $concat_string = $first_string.$second_string; # displaying concatination string result print "String After Concatenation = $concat_string\n"; |
Output:
String After Concatenation = GeeksforGeeks
Repetition Operator (x)
The x operator accepts a string on its left-hand side and a number on its right-hand side. It will return the string on the left-hand side repeated as many times as the value on the right-hand side. The repetition depends on the user’s input number.
Syntax:
"String" x number_of_times
Example:
# Perl program to demonstrate the # Repetition Operator (x) in String #!/usr/bin/perl # Input a string $string = "GeeksforGeeks "; # Repetation operator(x) $str_result = $string x 5; # Display output # print string 5 times print "$str_result"; |
Output:
GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks
Note: Possible cases while using the Repetition Operator (x) in String as follows:
- $string xnumber : Gives Output
- $string x number : Gives Output
- $stringxnumber : Gives Error(where no space between string and x)
- $stringx number : Gives Error(where no space between string and x)
Important Point to Remember: Both the Concatenation and Repetition operator can be used with assignment(=) operator as follows:
- Concatenation Assignment Operator (.=)
- Repetition Assignment Operator (x=)
Example:
# Perl program to demonstrate the # Concatenation and Repetition # Assignment Operator in String #!/usr/bin/perl # Input first string $string1 = "Geeks"; # Input second string $string2 = "forgeeks"; $combine = $string1; # combine two string function (.=) $combine .= $string2; # Display result print $combine; $str_result = "Sudo_Placements"; # Repetation operator(x) $str_result x= 5; # Display output # print string 5 times print "\n$str_result"; |
Output:
Geeksforgeeks Sudo_PlacementsSudo_PlacementsSudo_PlacementsSudo_PlacementsSudo_Placements
Auto-increment Operator (++)
This operator can also apply to strings. It is a unary operator thats why it will only take a single operand as string. The last character of the operand(i.e string) will increment by one using the ASCII values of characters. The important point to remember about ++ operator that if the string ends with ‘z or”Z’ then the result of ++ operator will be ‘a’ or ‘A’ respectively but the letter to the left of it will also increment by one as well.
Example:
# Perl program to demonstrate # Auto-increment Operator (++) #!/usr/bin/perl # Input string $st = "AYY"; $st++; # Display output print "After ++ : $st"; # Once again $st++; # Display output print "\nAgain After ++ : $st"; |
Output:
After ++ : AYZ Again After ++ : AZA
Recommended Posts:
- Perl | Useful String Operators
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- Perl | Operators | Set - 1
- Perl | Operators | Set - 2
- Perl | File Test Operators
- Perl | Operators in Regular Expression
- Perl | String functions (length, lc, uc, index, rindex)
- Perl | Automatic String to Number Conversion or Casting
- Perl | Useful String functions
- Perl | Extract IP Address from a String using Regex
- Perl | split() Function
- Perl | chomp() Function
- Perl | Backtracking in Regular Expression
- Perl | Searching in a File using regex
- Perl | lt operator
- Perl | chop() Function
- Perl | rename() Function
- Perl | Subroutines or Functions
- Perl | Decision Making (if, if-else, Nested–if, if-elsif ladder, unless, unless-else, unless-elsif)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

