Perl Characters and Strings
Perl Basics – Part 17
Perl Course
Foreword: In this part of the series, I explain the basics of Perl strings, and characters.
By: Chrysanthus Date Published: 29 Mar 2015
Introduction
Single Character
Examples of single characters are, A, B, C, a, b, c, 1, 2, 3. You also have some special keyboard single characters like, *, |, + .
Single Quotes and Single Characters
Everything being equal, you should type a single character in single quotes, making a string of one character. The above single characters would by typed as, 'A', 'B', 'C', 'a', 'b', 'c', '1', '2', '3', '*', '|', '+' . A single character can be assigned to a scalar variable. Try the following code that illustrates this:
use strict;
my ($var1, $var2, $var3, $var4, $var5, $var6) = ('B', 'a', '2', '*', '|', '+' );
print $var1, "\n", $var2, "\n", $var3, "\n", $var4, "\n", $var5, "\n", $var6;
You should have the following result:
B
a
2
*
|
+
Escape Sequences and Double Quotes
An escape sequence is a character made up of two characters: a backslash followed typically by an alphabet letter. The escape sequences you should already know are the whitespaces, which are, \t, \v, \ , \f, \r, \f . One can say an escape sequence is a special character made up of two characters: the backslash followed by a letter.
An escape sequence is not supposed to be printed. Its purpose is to create an effect. For example, \n is to send the cursor to the next line, below (in the console). If you type an escape sequence within single quotes, you will not have the desired effect; in this case the backslash and letter will be printed. For an escape sequence to have the desired effect, it has to be typed in double quotes. Try the following code:
use strict;
my $var0 = 1111;
my $var1 = 2222;
my $var2 = 3333;
print $var0; print '\n';
print $var1; print "\n";
print $var2;
You should have the following result:
1111\n2222
3333
Note that the first '\n' in single quotes did not have the desired effect; it should not have been printed, but it was printed. Any escape sequence in single quotes will always be printed, and that is not what you want. The second "\n" in double quotes was not printed, and it had the desired effect of sending the cursor to the next line and causing the next print to take place in the next line.
So always type an escape sequence within double quotes to have the desired effect. If you type an escape sequence within a string of double quotes containing single characters, it will still have its desired effect. Try the following:
use strict;
print "She is mine.\nI love her.";
The result is:
She is mine.
I love her.
If you type an escape sequence within a string of single quotes containing single characters, it will not have its desired effect; it will be printed. Try the following code:
use strict;
print 'She is mine.\nI love her.';
The result is:
She is mine.\nI love her.
not what you want.
Remember, a string in Perl is text within single quotes or double quotes. If the start quote is single, the end quote has to be single. If the start quote is double, the end quote has to be double. A scalar variable within a string of double quotes is expanded; that is, it is replaced by its literal value. Try the following code:
use strict;
my $money = 2000;
my $str = "I sent her $money for rents.";
print $str;
The result is:
I sent her 2000 for rents.
A scalar variable within a string of single quotes is not expanded. Try the following:
use strict;
my $money = 2000;
my $str = 'I sent her $money for rents.';
print $str;
The result is:
I sent her $money for rents.
not what you wanted because $money has not been replaced by its numerical value.
The $ Symbol within a String
Now, the dollar symbol, $ is used to start a scalar variable. So it is a special character, and cannot be used arbitrarily in a Perl code. If you want the dollar symbol within a string of double quotes to be printed, you have to escape it, making it, \$. Try the following code:
use strict;
my $money = 2000;
my $str = "I sent her \$$money for rents.";
print $str;
The result is:
I sent her $2000 for rents.
which is what you want.
A dollar symbol within a string of single quotes, escaped or not, is sent to the output, unchanged: Try the following:
use strict;
my $money = 2000;
my $str = 'I sent her \$$money for rents.';
print $str;
The result is:
I sent her \$$money for rents.
not what you want.
As you can see, an escape sequence should be typed within a string of double quotes to have its desired effect. Typing an escaped sequence within a string of single quotes simply returns the escaped sequence literal character, without its effect. A scalar variable within a string of double quotes is expanded. A scalar variable within a string of single quotes is not expanded.
Printing an Escape Sequence
An escaped sequence is meant to have its effect. So, how do you print its literal. You can print the literal by typing it within a string of single quotes; however, that is not good programming practice. To print an escaped sequence, use a string of double quotes and double the backslash; that is, escape the backslash. In this way, within the double quoted string, the escaped backslash is printed as one stroke, while the letter of the escaped sequence is printed as an ordinary character. Try the following:
use strict;
my $str = "The newline character is \\n, which is an escape sequence.";
print $str;
The result is:
The newline character is \n, which is an escape sequence.
A string is text within single quotes or double quotes. The recommended way to have a quotation within a string is to type the quotation within single quotes in a double quoted string. Try the following:
use strict;
my $str = "It is, 'There will be consequences.' said Obama.";
print $str;
The output is:
It is, 'There will be consequences.' said Obama.
You can do the reverse, by placing double quotes in single quotes (not recommended). Try the following:
use strict;
my $str = 'It is, "There will be consequences." said Obama.';
print $str;
The output is:
It is, "There will be consequences." said Obama.
This reverse approach is not recommended because of the issues of single quotes just returning their escape sequences without their effects, and they do not expand variables.
Concatenating Strings
You can join or concatenate strings using the dot operator. The strings can be in single or double quotes. The strings can also be in variables. Try the following code:
use strict;
print "She is mine." . ' I love her.' . "\n";
my $str1 = "She is mine.";
print $str1 . ' I love her.' . "\n";
my $str2 = ' I love her.';
print $str1 . $str2 . "\n";
The result is:
She is mine. I love her.
She is mine. I love her.
She is mine. I love her.
Before I conclude, know that in order to have a double quote within a double quoted string, you have to escape the double quote, making it \".
Conclusion
A string is text within double quotes or single quotes. An escape sequence should be typed within a string of double quotes to have its desired effect. Typing an escaped sequence within a string of single quotes simply returns the escaped sequence literal, without its effect. A scalar variable within a string of double quotes is expanded. A scalar variable within a string of single quotes is not expanded. To have a quotation within a string, type the quotation within single quotes in a double quoted string. You can concatenate strings using the dot operator.
See you in the next part of the series.
Chrys
Related Links
Perl BasicsPerl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course
BACK NEXT