Perl String: FAQ, Tutorial, Tips and Tricks for Strings in Perl

Strings are really what Perl's all about, so it's very easy to use strings in Perl.

Probably the three most important things to remember about strings in Perl are

  1. Strings are scalars, so they begin with the $ prefix.

  2. To join strings together, use . (dot), not + (plus)

  3. To test if two strings are identical, use eq not ==

Here's how you declare, initialize and print (display) a string in Perl:

use strict;
# use strict; forces you to declare
# all variables.

use warnings;
# use warnings; turns on
# warnings about uninitialized variables.

# Declare and initialize a string.
my $hello = "Hello there";

# print the string
print $hello;

If you want to print out several strings in Perl, or display some text made up from multiple strings, you have several options.

  • Join the strings together using the dot operator

  • Supply the strings to print as separate arguments

  • Embed the strings in a bigger string

Here are examples of all three options. Choosing the option that's best for you is largely a matter of deciding which option gives the clearest code for your particular usage.

use strict;
# use strict; forces you to declare
# all variables.

use warnings;
# use warnings; turns on
# warnings about uninitialized variables.

my $string1 = "Hello";
my $string2 = "Professor";

# Join $string1, some whitespace, $string2
# and a newline character; then supply the
# whole thing (one big string now) to print()
# as a single argument.
print $string1 . " " . $string2 . "\n";

# Send four separate arguments to print();
# $string1, some whitespace, $string2, 
# and a newline character.
print $string1, " ", $string2, "\n";

# Send a single argument to print();
# a single string that has other strings
# (including a newline character)
# embedded in it.
print "$string1 $string2\n";

Hello Professor
Hello Professor
Hello Professor

Now let's look at some of the most frequently-asked questions about strings in Perl.

Perl String Truncate: Truncating Strings in Perl

You can use substr() to truncate strings in Perl.

use strict;
use warnings;

my $string = q|
This is quite a long string,
which we have defined using the
highly-useful q operator. (Multi-
-line quote). You can use any 
character to open and close the 
quote, and you can also use qq
instead of q if you want variables
within your string to be
replaced with their values.

print substr($string, 0, 40);

This is quite a long string,
which we h

Perl Replace Spaces In Strings

Often people want to replace spaces in strings in Perl. There are several reasons why you might want to do this, such as:

  1. Getting rid of unwanted newline characters.

  2. Getting rid of consecutive spaces.

  3. Getting rid of spaces altogether.

Here are examples of all of the above.

use strict;
use warnings;

my $text = "This is  a string withn lots oftspaces";

# print the text.
print "1. $text\n";

# Remove newline and tab characters
# Actually, we'll replace them with
# spaces -- but we could just as easily
# remove them altogether.
$text =~ s/[\n\r\f\t]/ /g;
print "2. $text\n";

# Change consecutive spaces to single
# spaces.
$text =~ s/\s+/ /g;
print "3. $text\n";

# Remove spaces altogether.
$text =~ s/\s*//g;
print "4. $text\n";

1. This is  a string with
 lots of        spaces
2. This is  a string with  lots of spaces
3. This is a string with lots of spaces
4. Thisisastringwithlotsofspaces

Perl String Compare: Checking If Two Strings Are Equal

For this, you need eq.

my $string1 = "hello";
my $string2 = "hello";

if($string1 eq $string2) {
    print "Match!";


Perl String Length: Find the Size of a String in Perl

Use the length() function.

my $text = "this is a string";

print "Length of string: ", length($text), "\n";

Length of string: 16

Perl String Substitution: Replace Part of a String in Perl

To replace parts of a string in Perl, typically you'll want to use s///g.

See Perl Replace Substring for more details.

# The -g flag says, 'replace all occurences
# of the substitution found in the string'.
# For a case insenstive substitution, use 
# the -i flag.
$text =~ s/string/beetroot/g;

print "$text\n";

this is a beetroot

Perl String Matching: How To Search For Substrings in Strings

You can find substrings in Perl using the match operator, =~.

For more information on Perl regular expressions, check here

my $text = "this is a string containing the word dog";

if($text =~ /dog/) {
    print "Found a match!";
else {
    print "No match found.";

Found a match!

Perl String to Integer: Converting From Strings to Numbers and Back in Perl

In Perl, often when you think you need to convert a number to a string, you don't. Just use it as if it was a number.

my $number1 = "100";
my $number2 = "10";

print $number1 + $number2, "\n";

# You can also do this ...
print int($number1) + int($number2), "\n";


The same thing works in reverse; here we convert a number to a string by embedding it in another string, for example.

my $number = 10;

print "The number is $number\n";

The number is 10

If you need to convert numbers to string in a more precise way, you can resort to sprintf().

my $fraction = 10/3;
my $integer = 20;

my $text = sprintf("%.2f %d", $fraction, $integer);

print "$text\n";
# Could have used printf, of course!

3.33 20

You can find information about the various possible format strings here.

Perl String Concatenate: How to Append Strings to Strings in Perl

As mentioned above, to join strings use the dot operator. To append one string to another, use dot equals (.=)

my $string1 = "hello";
my $string2 = "there";

my $text = '';

# Join two strings together
# ($string1 and a string containing
# one space).
$text = $string1 . ' ';

# Append another string.
$text .= $string2;

print $text;

hello there

Perl Empty String

In Perl, variables can be undef (undefined). This is different to being set to zero (for a number) or empty (for a string).

Undefined and empty strings evaluate to FALSE in Perl; blank strings (containing spaces) evaluate to TRUE.

# Undefined string (set to undef)
my $string1;

# Empty string
my $string2 = '';

# Blank string.
my $string3 = ' ';

if($string1) {
    print "string 1 is set\n";
else {
    print "string 1 is not set\n";

if($string2) {
    print "string 2 is set\n";
else {
    print "string 2 is not set\n";

if($string3) {
    print "string 3 is set\n";
else {
    print "string 3 is not set\n";

string 1 is not set
string 2 is not set
string 3 is set

Any Other Questions?

That's it for now. If you have any questions about strings in Perl that I haven't answered here, feel free to leave a question in the comments and I'll answer as soon as I can.