Perl String

Strings are an essential part of the Perl language. They are scalar variables, so they start with ($) sign. A string can be defined within a single quote (') or double quote (").


Perl String Operators

The operators make it easy to manipulate a string in different ways. There are two types of string operators:

  • Concatenation (.)
  • Repetition (x)

Perl Concatenation Operator

Perl strings are concatenated with a (.) sign instead of (+) sign.

Output:

Christian Grey

Perl Repeitition Operator

Perl strings can be repeated a number of times with (x) variable.

Output:

Thank You Thank You Thank You

Perl Initializing and Declaring a String

In Perl, to declare a string use my keyword before variable name.

A string can be initialised and declared with the following syntax:

In this example, we have shown how to initialize and declare a string. We have printed several strings together by the following ways:

  • Join strings using a dot (.) operator.
  • Supply strings as separate arguments.
  • Embed strings in a bigger string.

We have shown all the three methods to print the output.

Output:

Welcome at JavaTpoint. This is our Perl Tutorial.
Welcome at JavaTpoint. This is our Perl Tutorial.
Welcome at JavaTpoint. This is our Perl Tutorial.

Perl Formatting Characters in string

CharacterDescription
\aBell
\bGives a backspace
\cXControl the characters. X is a character.
\eescape next character
\Eit ends \u, \l and \q function
\fGives formfedd to the string
\lTransformation of only next letter into lower case.
\LTransformation of all letters into lower case.
\nBegins next line from a new line
\0nnOctal format numbers are created
\QDo not match the pattern
\rGives a carriage return
\tGives a tab to the string
\uTransformation of only next letter into lower case.
\UTransformation of letters into uppercase.
\xnnHexadecimal format numbers are created

Perl Single quote Vs Double quote String

Strings can be placed within a single quote (') or double quote (") but they have little different behavior.

Output:

Hello $user, welcome at our site.\n
Hello Ana, welcome at our site today.

In single quote, all the characters are interpreted as it is.

Double quote provides interpolation. Means other variables present inside the string will represent their values. Escape characters will be replaced by their values like '\n' will display a new line.


Perl substr() Example

The substr() function is used to truncate the strings. We need to provide an offset string. String will be truncated till the provided offset value.

Mentioning length with the offset will print the string after offset value and till the mentioned length.

If you provide a new string with offset and length, it will replace the string after offset till the length value.

Output:

Our site javaTpoint provides all type of tutorials
site "javaTpoint" provides all type of tutorials
site javaTpoint
Our one and only site provides all type of tutorials

Perl String Comparison Example, eq

Perl strings are always compared with eq rather than (==). It checks whether two strings are equal or not.

Output:

Match!
Missmatch!

Perl Determining String Length, length()

Perl string length can be determined with length() function.

Output:

String Length : 50

Perl Replacing a string with another string, s///g

A string can be replaced with another string in two ways.

In first one, we have replaced Tigers with Lions which occurs single time in the string with s///.

In second one, we have replaced roses with flowers globally with s///g.

Output:

Lions are big and frightening.
Red flowers are very popular. Yellow flowers are less seen.

Perl Finding a match in the string, =~

Perl provides a match operator (=~) to find a substring from a string.

Output:

Matched
Match not Found

Perl Concatenating two Strings (.=)

Two strings can be joined together using (.=) operator.

Output:

Where there is a will, there is a way.