Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

String Literals in Perl

by root (Monk)
on Nov 05, 1999 at 02:26 UTC ( [id://945]=perltutorial: print w/replies, xml ) Need Help??

In perl there are two ways to represent string literals: single-quoted strings and double-quoted strings.

Single-Quoted Strings

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.
'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)
If you want to put a new line in a single-quoted string it goes something like this
'line1
line2'       #has eleven characters line1, newline character, and then line2
Single-quoted strings don't interpret \n as a newline.

Double-Quoted Strings
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
$word="hello";             #$word becomes hello
$statement="$word world";  #variable interpolation, $statement becomes "hello world"
"Hello World\n";           #"Hello World" followed by a newline

Some of the things you can put in a Double-Quoted String
RepresentationWhat it Means
\aBell
\bBackspace
\eEscape
\fFormfeed
\nNewline
\rReturn
\tTab
\\Backslash
\"Double quote
\007octal ascii value this time 007 or the bell
\x07hex ascii value this time 007 or the bell
\cDany control character.. here it is control-D
\llowercase next letter
\uuppercase next letter
\Llowercase all letters until \E
\Uuppercase all letters until \E
\QBackslash quote all nonletters and nonnumbers until \E
\EStop \U \L or \Q

Replies are listed 'Best First'.
RE: String Literals in Perl
by tye (Sage) on Aug 02, 2000 at 19:50 UTC

    A fine node with one mistake:

    print 'blah\\blah',"\n";

    prints "blah\blah". To put a literal \ in a Perl single-quoted string, you should use \\, just like with double-quoted strings. If the character after a \ (in a single-quoted string) doesn't happen to be ' or \, then the \ is preserved. But using this fact often leads to confusion like the mistake in your post, so I discourage people from doing that.

    I really wish Perl had used '' (two adjacents 's) to quote ' inside of 's. But my time machine is broken.

Typo in Table - RE: String Literals in Perl
by Anonymous Monk on Nov 04, 2000 at 17:47 UTC

    Hi,
    In the table "Some of the things you can put in a Double-Quoted String" there seems to be a typo as follows :

    In your Table is :

    \L lowercase all letters until \E \U uppercase all letters until \E \Q Backslash quote all nonletters and nonnumbers until \E \E Stop \U \L or \E It seems that the line \E Stop \U \L or \E should read \E Stop \U \L or \Q . Otherwise it means the \E stops it self .

    Sincerely,
    Alon Cohen
    alon@jerusalemexport.com

    Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

      Thanks. I fixed it. And it only took 6 months. /:

              - tye (but my friends call me "Tye")
RE: String Literals in Perl
by anguyenperl (Initiate) on Aug 02, 2000 at 07:14 UTC
    $mystring="anguyenper"
Re: String Literals in Perl
by Anonymous Monk on Dec 05, 2001 at 21:34 UTC
    hello\cZathis is a test  
      'hi\there' #has eight characters and represents "hi\there"
      shouldn't be:
      hi<tab here>here <--- with the tab inside?

        The perl string literal 'hi\there' has 8 characters: h, i, \, t, h, e, r, e

        The perl string literal "hi\there" has 7 characters: h, i, <Tab>, h, e, r, e

        -- Hofmator

        'hi\there' #has eight characters and represents"hi\there"

        was confusing so I updated it to:

        'hi\there' #has eight characters and represents"hi\\there" (one \ in the string)

        More importantly, however:

        'blah\\blah' #has ten characters and represents "blah\\blah"

        was incorrect so I updated it to

        'blah\\blah' #has nine characters and represents "blah\\blah" (one \ in the string)

        Thanks for the note. Next time, please don't remove the "Re^X:" from the front of the title.

        - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perltutorial [id://945]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2024-03-19 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found