next up previous contents
Next: Example: Double-Quoted Strings Up: String Literals Previous: String Literals

Example: Single-Quoted Strings

The following examples show you how to use string literals. String literals are widely used to identify filenames or when messages are displayed to users. First, we'll look at single-quoted strings, then double-quoted strings.

A single-quoted string is pretty simple. Just surround the text that you'd like to use with single quotes, E.g.

'David Marshall'

Strings are pretty simple. But what if you wanted to use a single quote inside the literal? If you did this, Perl would think you wanted to end the string early and a compiler error would result. Perl uses the backslash (\) character to indicate that the normal function of the single quote-ending a literal-should be ignored for a moment.

Tip The backslash character is also called an escape character-perhaps because it lets the next character escape from its normal interpretation

A literal is given below. Notice how the single quote is used.

So for example we can do

'These are David Marshall\'s Course Notes'

or more advanced we can do

'Fiona asked: "Are you enjoying David\'s Perl Course"'

The single-quotes are used here specifically so that the double-quotes can be used to surround the spoken words. Later in the section on double-quoted literals, you'll see that the single-quotes can be replaced by double-quotes if you'd like. You must know only one more thing about single-quoted strings. You can add a line break to a single-quoted string simply by adding line breaks to your source code-as demonstrated by below:

print 'Bill of Goods

Bread:    $34 .45

Fruit:    $45.00

         ======

         $79.45'

The basic outline of the code here is:


next up previous contents
Next: Example: Double-Quoted Strings Up: String Literals Previous: String Literals
dave@cs.cf.ac.uk