String is a sequence of characters. As we have discussed in the Scalars guide that string is considered as scalar variable in Perl programming language. There are two ways to define a string in Perl using ‘(single quotes) and ” (double quotes).
1. String Declaration and Initialization in Perl
Since Strings are scalars, their name starts with $
For example:
my $website = "BeginnersBook"; my $editor = "Chaitanya"; print "$editor works for $website";
Output:
Chaitanya works for BeginnersBook
2 String interpolation – Single vs double quotes
I have already covered this topic in Scalars in Perl article.
Quotes are not part of the string, they just specify the beginning and the ending of the string. You may think that both single and double quotes serve the same purpose, however this is not true. They are used in different-2 cases. To understand the difference between these two, lets have a look at the following example.
#!/usr/bin/perl print "Welcome to\t Beginnersbook.com\n"; print 'Welcome to\t Beginnersbook.com\n'; This would produce the following output: Welcome to Beginnersbook.com Welcome to\t Beginnersbook.com\n
You can clearly see the difference that double quotes interpolated escape sequences “\t” and “\n” however single quotes did not.
Note: We will discuss escape sequences in detail in the next tutorial.
Example 2: Single vs Double quotes:
#!/usr/bin/perl $website = "Beginnersbook"; print "Website Name: $website\n"; print 'Website Name: $website\n';
Output:
Website Name: Beginnersbook Website Name: $website\n
This is another advantage of double quotes as they are “variable interpolated”. Which means that variable name inside double quotes are replaced with their values. This doesn’t happen in single quoted string.
2.1 Use of Single quotes
You may be thinking of avoiding single quotes in perl program, however there are certain cases where you would want to use single quotes.
For e.g. If you want to store email address in a variable, in that case double quotes would throw an error, you need to use single quote in that case. For e.g.
$email = "[email protected]"; would throw an error $email = '[email protected]'; would work fine.
The issue here is that @gmail is interpolated as an array. The variables starting with @ symbol are interpolated as arrays. We can also avoid the error with double quotes by using \ escape sequence, which we have discussed in the next section.
2.2 Backslash in Perl
The backslash can perform one of two tasks:
1) it takes away the special meaning of the character following it. For e.g. print “\$myvar”; would produce $myvar output instead of variable myvar value because the backslash(\) just before the $ takes away the special meaning of $
2) It is the start of a backslash or escape sequence. For e.g. \n is an escape sequence used for newline
2.2.1 what is the use of it?
Believe me, you would use it frequently while developing an application in perl. Lets say you want to print the string that contains double quotes. For e.g. If i want to print: Hello This is “My blog” then I would need to use following logic:
#!/usr/bin/perl $msg = "Hello This is \"My blog\""; print "$msg\n";
Output:
Hello This is "My blog"
3. String Operations
Lets see the operations that we can perform on the strings.
3.1 Concatenation
To concatenate the string use dot(.) operator. In the following example we are concatenating three strings, $str1, white space and $str2.
$str1 = "Cherry"; $str2 = "Coco"; $mystr = $str1 . " " . $str2; print "$mystr\n";
Output:
Cherry Coco
3.2 Repetition
To repeat a string with a specified number of times, use character x followed by the number. In the following example we have used the number 4 after character x that is why the string is repeated four number of times in the output.
$str = "Cherry"; $mystr = $str x 4; print "$mystr\n";
Output:
CherryCherryCherryCherry
3.3 Getting the substring – substr() function
The substr() function is used for getting a substring from a given string.
use strict; use warnings; # This is our original string my $str = "I know who you are, I will find you"; print "My original String is: $str\n"; # substring - starting from 8th char till the end of string my $substr1 = substr($str, 7); print "My First substring is: $substr1\n"; # substring - starting from 8th char and length of 11 my $substr2 = substr($str, 7, 11); print "My Second substring is: $substr2\n";
Output:
My original String is: I know who you are, I will find you My First substring is: who you are, I will find you My Second substring is: who you are
This same function can be used for replacing a part of string with the new content. Lets take an example to understand this:
use strict; use warnings; # This is our original string my $str = "I know who you are, I will find you"; print "My original String is: $str\n"; my $newstr = substr($str, 7, 11, "you are the killer"); print "My updated String is: $str\n"
Output:
My original String is: I know who you are, I will find you My updated String is: I know you are the killer, I will find you
3.4 Finding the length of the String in Perl – length() function
To find the length of a string in Perl, use length() function. This function counts the number of characters in a string(including the white spaces) and returns it.
use strict; use warnings; my $str = "I know who you are, I will find you"; print "length of my string is:", length($str);
Output:
length of my string is:35
3.5 String comparison in Perl
To compare two strings we use the eq operator in Perl.
use strict; use warnings; my $str = "hello"; my $str2 = "hello"; if ($str eq $str2){ print "str and str2 are same\n"; }
Output:
str and str2 are same
Leave a Reply