Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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

In reply to String Literals in Perl by root

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-25 14:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found