The other type of scalar available to us is the string, and we’ve already seen a few examples of them. In the last chapter, we met the string "Hello, world!\n". A string is a series of characters surrounded by some sort of quotation marks. Strings can contain ASCII (or Unicode) data and escape sequences such as the\nof our example, and there is no maximum length restriction on a string imposed by Perl. Practically speaking there is a limit imposed by the amount of memory in your computer, but it’s quite hard to hit.
Single- vs Double-Quoted Strings
The quotation marks you choose for your string are significant. So far we’ve only seen double-quoted strings, like this:"Hello, world!\n". There is another type of string—one which has been single-quoted. Predictably, they are surrounded by single quotes:''. The important difference is that no processing is done within single-quoted strings, except on\\and\'. We’ll also see later that variable names inside double-quoted strings are replaced by their contents, whereas single-quoted strings treat them as ordinary text. We call both these types of processing interpolation, and say that single-quoted strings are not interpolated.
Consider the following program, bearing in mind that\tis the escape sequence that represents a tab:
print '\tThis is a single quoted string.\n'; print "\tThis is a double quoted string.\n";
The double-quoted string will have its escape sequences processed, and the single-quoted string will not. The output is
$ perl quotes.pl
What do we do if we want to have a backslash in a string? This is a common concern for Windows users, as a Windows path looks something like this: C:\WINNT\Profiles\ . . . In a double-quoted string, a backslash will start an escape sequence, which is not what we want it to do.
There is, of course, more than one way to do it. We can either use a single-quoted string, as shown previously, or we can escape the backslash. One principle that we’ll see often in Perl, and especially when we get to regular expressions, is that we can use a backslash to turn off any special effect a character may have. This operation is called escaping, or more commonly, backwhacking.
In this case, we want to turn off the special effect a backslash has, and so we escape it:
This prints the following:
$ perl quotes2.pl
Aha! Some of you may have got this message instead:
Can't find string terminator " ' " anywhere before EOF at quotes2.pl line 5.
The reason for this is that you have probably left out the space character in line 5 before the second single quote. Remember that\'tells Perl to escape the single quote, and so it merrily heads off to look for the next quote, which of course is not there. Try this program to see how Perl treats these special cases:
print 'ex\\ er\' , ' ci\' se\'' , "\n";
The output you get this time is
$ perl aside1.pl
Can you see how Perl did this? Well, we simply escaped the backslashes and single quotes. It will help you to sort out what is happening if you look at each element individually. Remember, there are three arguments in this example. Don’t let all the quotes confuse you.
Actually, there’s an altogether sneakier way of doing it. Internally, Windows allows you to separate paths in the Unix style with a forward slash, instead of a backslash. If you’re referring to directories in Perl on Windows, you may find it easier to sayC:/WINNT/Profiles/instead. This allows you to get the variable interpolation of double-quoted strings without the “Leaning Toothpick Syndrome” of multiple backslashes.
So much for backslashes, what about quotation marks? The trick is making sure Perl knows where the end of the string is. Naturally, there’s no problem with putting single quotes inside a double-quoted string, or vice versa:
print "It's as easy as that.\n";
This will produce the quotation marks in the right places:
$ perl quotes3.pl
The trick comes when we want to have double quotes inside a double-quoted string or single quotes inside a single-quoted string. As you might have guessed, though, the solution is to escape the quotes on the inside. Suppose we want to print out the following quote, including both sets of quotation marks:
'"Hi," said Jack. "Have you read Slashdot today?"'
Here’s a way of doing it with a double-quoted string:
print "'"Hi," said Jack. "Have you read Slashdot today?"'\n";
Now see if you can modify this to make it a single-quoted string—don’t forget that\nneeds to go in separate double quotes to make it interpolate.
q// and qq//
It would be nice if you could select a completely different set of quotes so that there would be no ambiguity and no need to escape any quotes inside the text. The first operators we’re going to meet are the quote-like operators that do this for us. They’re written asq//andqq//, the first acting like a single-quoted string, and the second like a double-quoted string. Now instead of the preceding, we can write
print qq/'"Hi," said Jack. "Have you read Slashdot today?"'\n/;
Please check back next week for the continuation of this article.
blog comments powered by Disqus