<?xml version="1.0" encoding="windows-1252"?>
<node id="945" title="String Literals in Perl" created="1999-11-04 21:26:44" updated="2005-08-15 10:01:55">
<type id="956">
perltutorial</type>
<author id="113">
root</author>
<data>
<field name="doctext">
In perl there are two ways to represent string literals: single-quoted strings and double-quoted strings.&lt;BR&gt;
&lt;BR&gt;&lt;B&gt;Single-Quoted Strings&lt;/B&gt;&lt;BR&gt;&lt;BR&gt;
Single quoted are a sequence of characters that begin and end with a single quote.  These quotes are not a part of the string they just mark the beginning and end for the Perl interpreter.  If you want a ' inside of your string you need to preclude it with a \ like this \' as you'll see below.
Let's see how this works below.&lt;BR&gt;
&lt;PRE&gt;
'four'       #has four letters in the string
'can\'t'     #has five characters and represents "can't"
'hi\there'   #has eight characters and represents"hi\\there" (one \ in the string)
'blah\\blah' #has nine characters and represents "blah\\blah" (one \ in the string)
&lt;/PRE&gt;
If you want to put a new line in a single-quoted string it goes something like this&lt;BR&gt;
&lt;PRE&gt;
'line1
line2'       #has eleven characters line1, newline character, and then line2
&lt;/PRE&gt;
Single-quoted strings don't interpret \n as a newline.
&lt;BR&gt;&lt;BR&gt;
&lt;B&gt;Double-Quoted Strings&lt;/B&gt;&lt;BR&gt; 
Double quoted strings act more like strings in C or C++ the
backslash allows you to represent control characters.  Another
nice feature Double-Quoted strings offers is variable interpolation
this substitutes the value of a variable into the string. Some examples are below&lt;BR&gt;
&lt;PRE&gt;
$word="hello";             #$word becomes hello
$statement="$word world";  #variable interpolation, $statement becomes "hello world"
"Hello World\n";           #"Hello World" followed by a newline
&lt;/PRE&gt;
&lt;BR&gt;
&lt;B&gt;Some of the things you can put in a Double-Quoted String&lt;/B&gt;&lt;BR&gt;
&lt;TABLE BORDER=1&gt;
&lt;TR&gt;&lt;TD&gt;Representation&lt;/TD&gt;&lt;TD&gt;What it Means&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\a&lt;/TD&gt;&lt;TD&gt;Bell&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\b&lt;/TD&gt;&lt;TD&gt;Backspace&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\e&lt;/TD&gt;&lt;TD&gt;Escape&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\f&lt;/TD&gt;&lt;TD&gt;Formfeed&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\n&lt;/TD&gt;&lt;TD&gt;Newline&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\r&lt;/TD&gt;&lt;TD&gt;Return&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\t&lt;/TD&gt;&lt;TD&gt;Tab&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\\&lt;/TD&gt;&lt;TD&gt;Backslash&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\"&lt;/TD&gt;&lt;TD&gt;Double quote&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\007&lt;/TD&gt;&lt;TD&gt;octal ascii value this time 007 or the bell&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\x07&lt;/TD&gt;&lt;TD&gt;hex ascii value this time 007 or the bell&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\cD&lt;/TD&gt;&lt;TD&gt;any control character.. here it is control-D&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\l&lt;/TD&gt;&lt;TD&gt;lowercase next letter&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\u&lt;/TD&gt;&lt;TD&gt;uppercase next letter&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\L&lt;/TD&gt;&lt;TD&gt;lowercase all letters until \E&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\U&lt;/TD&gt;&lt;TD&gt;uppercase all letters until \E&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\Q&lt;/TD&gt;&lt;TD&gt;Backslash quote all nonletters and nonnumbers until \E&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;\E&lt;/TD&gt;&lt;TD&gt;Stop \U \L or \Q&lt;/TD&gt;&lt;/TR&gt;
&lt;/TABLE&gt;

</field>
</data>
</node>
