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
In Perl, string manipulation is a fundamental aspect of the language, given its roots in text processing. Perl provides a rich set of functions for string operations. This tutorial provides an overview of the essential string functions in Perl.
length
Returns the length of a string.
my $str = "Hello, world!"; my $len = length($str); # $len is 13
.
Combine two strings.
my $greeting = "Hello, " . "world!"; # "Hello, world!"
x
Repeats a string a given number of times.
my $repeated = "ha" x 3; # "hahaha"
substr
Extracts part of a string.
my $extract = substr("Hello, world!", 0, 5); # "Hello"
You can also use substr
to modify parts of a string.
substr($str, 7, 5) = "Planet"; # $str is now "Hello, Planet!"
lc
, uc
, lcfirst
, and ucfirst
lc
: Convert to lowercase.uc
: Convert to uppercase.lcfirst
: Convert the first character to lowercase.ucfirst
: Convert the first character to uppercase.print lc("HELLO"); # "hello" print uc("world"); # "WORLD" print ucfirst("hello"); # "Hello" print lcfirst("WORLD"); # "wORLD"
index
and rindex
index
: Find the position of a substring.rindex
: Find the last position of a substring.my $position = index("apple pie", "pie"); # 6 my $last_pos = rindex("apple pie pie", "pie"); # 10
s///
operatorReplace parts of a string using regular expressions.
my $text = "Hello, world!"; $text =~ s/world/Perl/; # $text is now "Hello, Perl!"
split
and join
split
: Breaks a string into a list of substrings.join
: Combines a list of strings into a single string.my @words = split(/ /, "Hello, world!"); my $sentence = join(" ", @words);
q
and qq
q
: Single quote a string.qq
: Double quote a string.my $single = q(This is a 'single' quoted string); my $double = qq(This is a "double" quoted string);
chomp
and chop
chomp
: Removes trailing newline or a specific trailing string.chop
: Removes the last character from a string.my $input = "Hello\n"; chomp($input); # $input is now "Hello" my $word = "Hello"; chop($word); # $word is now "Hell"
printf
and sprintf
Format strings with specific layouts.
printf("I have %d apples.", 5); # Prints "I have 5 apples." my $formatted = sprintf("I have %d apples.", 5); # "I have 5 apples."
Perl's rich set of string functions makes it a powerful language for text processing tasks. By mastering these functions, you can efficiently manipulate, format, and analyze textual data in your Perl programs.
Manipulating strings in Perl:
my $string = "Hello, Perl!"; # Manipulating strings my $uppercase = uc($string); my $lowercase = lc($string);
String concatenation in Perl:
.
).my $first_name = "John"; my $last_name = "Doe"; my $full_name = $first_name . " " . $last_name;
Substring extraction in Perl:
substr
.my $sentence = "Perl is powerful"; my $substring = substr($sentence, 5, 2); # Extracts "is"
String length function in Perl:
length
.my $word = "Perl"; my $length = length($word);
String comparison 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"; }
Searching and replacing in strings in Perl:
index
and s///
.my $sentence = "Perl is fun and Perl is powerful"; # Searching for a substring my $position = index($sentence, "Perl"); # Replacing a substring $sentence =~ s/Perl/Python/;
String splitting and joining in Perl:
split
and join
.my $csv_data = "John,Doe,30"; # Splitting a CSV string my @fields = split(',', $csv_data); # Joining array elements into a string my $joined_data = join('-', @fields);
Regular expressions for string manipulation in Perl:
my $text = "The quick brown fox jumps over the lazy dog"; # Matching words starting with 'b' my @matches = $text =~ /\b[bB]\w+/g;
Case conversion functions in Perl strings:
ucfirst
, lcfirst
, uc
, lc
.my $name = "alice"; # Capitalizing the first letter my $capitalized = ucfirst($name); # Converting to uppercase my $uppercase_name = uc($name);