Beginner Perl Maven tutorial

  • Installing and getting started with Perl
  • The Hash-bang line, or how to make a Perl scripts executable on Linux
  • Perl Editor
  • How to get Help for Perl?
  • Perl on the command line
  • Core Perl documentation and CPAN module documentation
  • POD - Plain Old Documentation
  • Debugging Perl scripts
  • Common Warnings and Error messages in Perl
  • Prompt, read from STDIN, read from the keyboard in Perl
  • Automatic string to number conversion or casting in Perl
  • Conditional statements, using if, else, elsif in Perl
  • Boolean values in Perl
  • Numerical operators

String operators: concatenation (.), repetition (x)

  • undef, the initial value and the defined function of Perl
  • Strings in Perl: quoted, interpolated and escaped
  • Here documents, or how to create multi-line strings in Perl
  • Scalar variables
  • Comparing scalars in Perl
  • String functions: length, lc, uc, index, substr
  • Number Guessing game
  • Scope of variables in Perl
  • Short-circuit in boolean expressions
  • How to exit from a Perl script?
  • Standard output, standard error and command line redirection
  • Warning when something goes wrong
  • What does die do?
  • Writing to files with Perl
  • Appending to files
  • Open and read from text files
  • Don't Open Files in the old way
  • Reading and writing binary files in Perl
  • EOF - End of file in Perl
  • tell how far have we read a file
  • seek - move the position in the filehandle in Perl
  • slurp mode - reading a file in one step
  • Perl for loop explained with examples
  • Perl Arrays
  • Processing command line arguments - @ARGV in Perl
  • How to process command line arguments in Perl using Getopt::Long
  • Advanced usage of Getopt::Long for accepting command line arguments
  • Perl split - to cut up a string into pieces
  • How to read a CSV file using Perl?
  • The year of 19100
  • Scalar and List context in Perl, the size of an array
  • Reading from a file in scalar and list context
  • STDIN in scalar and list context
  • Sorting arrays in Perl
  • Sorting mixed strings
  • Unique values in an array in Perl
  • Manipulating Perl arrays: shift, unshift, push, pop
  • Reverse Polish Calculator in Perl using a stack
  • Using a queue in Perl
  • Reverse an array, a string or a number
  • The ternary operator in Perl
  • Loop controls: next, last, continue, break
  • min, max, sum in Perl using List::Util
  • qw - quote word
  • Subroutines and functions in Perl
  • Passing multiple parameters to a function in Perl
  • Variable number of parameters in Perl subroutines
  • Returning multiple values or a list from a subroutine in Perl
  • Understanding recursive subroutines - traversing a directory tree
  • Hashes in Perl
  • Creating a hash from an array in Perl
  • Perl hash in scalar and list context
  • exists - check if a key exists in a hash
  • delete an element from a hash
  • How to sort a hash in Perl?
  • Count the frequency of words in text using Perl
  • Introduction to Regexes in Perl 5
  • Regex character classes
  • Regex: special character classes
  • Perl 5 Regex Quantifiers
  • trim - removing leading and trailing white spaces with Perl
  • Perl 5 Regex Cheat sheet
  • What are -e, -z, -s, -M, -A, -C, -r, -w, -x, -o, -f, -d , -l in Perl?
  • Current working directory in Perl (cwd, pwd)
  • Running external programs from Perl with system
  • qx or backticks - running external command and capturing the output
  • How to remove, copy or rename a file with Perl
  • Reading the content of a directory
  • Traversing the filesystem - using a queue
  • Download and install Perl
  • Installing a Perl Module from CPAN on Windows, Linux and Mac OSX
  • How to change @INC to find Perl modules in non-standard locations
  • How to add a relative directory to @INC
  • How to replace a string in a file with Perl
  • How to read an Excel file in Perl
  • How to create an Excel file with Perl?
  • Sending HTML e-mail using Email::Stuffer
  • Perl/CGI script with Apache2
  • JSON in Perl
  • Simple Database access using Perl DBI and SQL
  • Reading from LDAP in Perl using Net::LDAP
  • Global symbol requires explicit package name
  • Variable declaration in Perl
  • Use of uninitialized value
  • Barewords in Perl
  • Name "main::x" used only once: possible typo at ...
  • Unknown warnings category
  • Can't use string (...) as an HASH ref while "strict refs" in use at ...
  • Symbolic references in Perl
  • Can't locate ... in @INC
  • Scalar found where operator expected
  • "my" variable masks earlier declaration in same scope
  • Can't call method ... on unblessed reference
  • Argument ... isn't numeric in numeric ...
  • Can't locate object method "..." via package "1" (perhaps you forgot to load "1"?)
  • Useless use of hash element in void context
  • Useless use of private variable in void context
  • readline() on closed filehandle in Perl
  • Possible precedence issue with control flow operator
  • Scalar value ... better written as ...
  • substr outside of string at ...
  • Have exceeded the maximum number of attempts (1000) to open temp file/dir
  • Use of implicit split to @_ is deprecated ...
  • Multi dimensional arrays in Perl
  • Multi dimensional hashes in Perl
  • Minimal requirement to build a sane CPAN package
  • Statement modifiers: reversed if statements
  • What is autovivification?
  • Formatted printing in Perl using printf and sprintf

When interpolation is not the answer

X the repetition operator, ++ on a string.

Gabor Szabo

Published on 2014-05-01

Author: Gabor Szabo

perl concatenation assignment operator

  • about the translations
  • Trending Now
  • Foundational Courses
  • Data Science
  • Practice Problem
  • Machine Learning
  • System Design
  • DevOps Tutorial

Perl | String Operators

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:  

Output:    

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:  

Output:  

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:  

   

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:    

   

What will happen if we pass an Alpha Numeric string with a number at last and try to increment it ? The number will be incremented by one just like it happened with the previous example –

Time Complexity – O(1)

Auxiliary Space – O(1)

author

Please Login to comment...

Similar reads.

  • perl-operators
  • Perl-String

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

alvin alexander

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 you’ll 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, Solution #1: Using ${name}

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

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, Solution #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:

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, Solution #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:

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".

Help keep this website running!

  • How to concatenate strings in Perl
  • Java String concatenation: How to combine (merge) two Strings
  • AppleScript string tip: How to concatenate strings
  • Scala: How to concatenate two multiline strings into a resulting multiline string
  • Use Perl here documents to print multiple lines of output

books by alvin

  • My free Scala and ZIO 2 training videos
  • Addiction: When an addict gets to the point where they want to stop, the chemical addition is still very strong
  • Alan Watts quote: Problems that remain persistently insoluble should always be suspected ...
  • Mooncat Book Company in Mt. Sterling, Kentucky (and the Scala Cookbook)
  • I need your grace