Scalar data in Perl is considered as singular, it can either be number or string. We have covered a little bit about scalars in variables and data types tutorials. In this tutorial we will discuss few important things about strings, we would skip numbers as use of them are pretty straight forward.
Single quoted string vs double quoted string
Quotes are not part of the string, they just specify the beginning and the ending of the string. In Perl, single and double quotes behaves differently. To understand the difference, lets have a look at the code below:
#!/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.
#!/usr/bin/perl $website = "Beginnersbook"; print "Website Name: $website\n"; print 'Website Name: $website\n';
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.
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.
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
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";
Hello This is "My blog"
You can concatenate strings using dot( . ) operator. For example:
"Hello"."Chaitanya" # Equivalent to "HelloChaitanya" "Hello"."Chaitanya"."\n" # Equivalent to "HelloChaitanya\n"
Syntax: MyString x MyNumber
This would repeat the string “MyString” by “MyNumber” times.
Here x should be in lowercase.
"book" x 4 # Same as "bookbookbookbook" "Tom" x (1+2) # Same as "Tom" x 3 or "TomTomTom" "Wow" x 2.7 # Same as "Wow" x 2 or "WowWow"
In the third example 2.7 is considered 2, no number round off happens here.
Note: Perl considers the part before x operator as string and part after x is treated as number. In other words you can say that the left operand is treated as string and right operand as number.
8 x 3 # Same as "8" x 3 or "888"
You must remember this point while using x operator to avoid any confusion. 8 x 3 is not 24 instead it is “888”.
Leave a Reply