Home arrow Perl Programming arrow Page 4 - Scalars

Strings - Perl

If you're writing a program that involves computation, you're going to come face to face with some basic programming concepts. This article, the first of a five-part series, will explain scalars, one of the basic ideas of programming in Perl. It is excerpted from chapter two of the book Beginning Perl, written by James Lee (Apress; ISBN: 159059391X).

  1. Scalars
  2. Numbers
  3. Binary, Hexadecimal, and Octal Numbers
  4. Strings
By: Apress Publishing
Rating: starstarstarstarstar / 2
March 25, 2010

print this article



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:

#!/usr/bin/perl -w
# quotes.pl

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
\tThis is a single quoted string.\n    This is a double quoted string.

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:

#!/usr/bin/perl -w
# quotes2.pl

print "C:\\WINNT\\Profiles\\\n";
print 'C:\WINNT\Profiles\ ', "\n";

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:

#!/usr/bin/perl -w
# aside1.pl

print 'ex\\ er\' , ' ci\' se\'' , "\n";

The output you get this time is

$ perl aside1.pl
ex\ er\ ci' se'

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:

#!/usr/bin/perl -w
# quotes3.pl

print "It's as easy as that.\n";
print '"Stop," he cried.', "\n";

This will produce the quotation marks in the right places:

$ perl quotes3.pl
It's as easy as that.
"Stop," he cried.

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:

#!/usr/bin/perl -w
# quotes4.pl

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

#!/usr/bin/perl -w
# quotes5.pl

print qq/'"Hi," said Jack. "Have you read Slashdot today?"'\n/;

Please check back next week for the continuation of this article.

>>> More Perl Programming Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: