Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
Strings are a fundamental part of Perl, and the language provides several operators to facilitate their manipulation. This tutorial will cover the primary string operators available in Perl.
.
The .
operator concatenates two strings.
my $str1 = "Hello"; my $str2 = "World"; my $combined = $str1 . " " . $str2; # "Hello World"
x
The x
operator repeats a string a specified number of times.
my $repeated = "ha" x 3; # "hahaha"
Perl provides separate operators for numeric and string comparison. For strings:
eq
ne
lt
gt
le
ge
if ($str1 eq $str2) { print "Both strings are equal!"; }
q
and qq
q
: It's equivalent to single quotes in Perl. It doesn't interpolate variables.qq
: It's equivalent to double quotes. It interpolates variables.my $name = "Alice"; print q(Hello, $name!); # Outputs: Hello, $name! print qq(Hello, $name!); # Outputs: Hello, Alice!
<<
Used to define multi-line strings.
my $long_string = <<END; This is a long string. It spans multiple lines. END
END
can be any delimiter. The string starts after the <<END
and continues until the delimiter (END
in this case) is found at the beginning of a line.
Backticks execute a command and capture its output as a string.
my $output = `ls -l`; print $output;
Variables are automatically expanded inside double-quoted strings.
my $fruit = "apple"; print "I have an $fruit.\n"; # I have an apple.
Escape sequences like \n
(newline), \t
(tab), etc., are also interpolated inside double-quoted strings.
Used within double-quoted strings and some other delimiters.
\n
: newline\t
: tab\\
: backslash\"
: double quoteprint "Hello\tWorld\n"; # Hello World (with a tab between Hello and World)
length
Though technically a function, length
is crucial when dealing with strings as it returns the length of a string.
my $length = length("Hello"); # 5
The ++
operator can also work on strings, incrementing to the next logical string.
my $str = "a"; $str++; # "b" my $str2 = "z"; $str2++; # "aa"
Perl's string operators make it a formidable language for text processing and string manipulations. Understanding how to utilize these operators can help you efficiently tackle text-based tasks in your Perl scripts.
Concatenation operator (.) in Perl:
.
) operator.my $greeting = "Hello, "; my $name = "Perl"; my $message = $greeting . $name;
Repetition operator (x) in Perl strings:
x
).my $stars = "*" x 5; # Repeats '*' five times
String interpolation operator in Perl:
my $language = "Perl"; my $message = "I love $language!";
Comparison operators for strings in Perl:
eq
, ne
, lt
, le
, gt
, ge
.my $str1 = "Perl"; my $str2 = "Python"; if ($str1 eq $str2) { print "Strings are equal.\n"; } else { print "Strings are not equal.\n"; }
Perl string operators and variables:
my $prefix = "Hello"; my $suffix = "Perl"; my $result = $prefix . ", " . $suffix;
Logical operators with strings in Perl:
&&
, ||
).my $str1 = "Perl"; my $str2 = "Python"; if ($str1 eq "Perl" && $str2 ne "Perl") { print "Condition is true.\n"; }
String concatenation vs. interpolation in Perl:
my $name = "Perl"; # Concatenation my $message_concat = "Hello, " . $name; # Interpolation my $message_interp = "Hello, $name";
Overloading string operators in Perl:
package MyClass; use overload '.' => \&concatenate, 'eq' => \= sub new { my ($class, $value) = @_; return bless { value => $value }, $class; } sub concatenate { my ($self, $other, $swap) = @_; return MyClass->new($self->{value} . $other->{value}); } sub equals { my ($self, $other, $swap) = @_; return $self->{value} eq $other->{value}; }