Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Problem w/ single-quoted strings

by Stevie-O (Friar)
on May 13, 2004 at 21:46 UTC ( #353219=note: print w/replies, xml ) Need Help??

in reply to Problem w/ single-quoted strings

OK, first off, I'm a bit confused, because your first line says 'Testing\z'. Perhaps you meant 'Testing\n' ?

With that out of the way, you are missing something -- or rather, the document you just quoted is missing something.

It handles three cases:

  1. Any character OTHER than a backslash or termination character is inserted literally. (The termination character is normally ' but may differ if q// is used.)
  2. Any backslash followed by the termination character inserts the termination character.
  3. Any backslash followed by a backslash inserts a backslash.
What it doesn't tell you is the fourth case:
  1. Any backslash followed by a character OTHER than the termination character or a backslash inserts a backslash and that literal character.
The reason for the double-backslash thing is in case you wanted to include a backslash at the end of the string. For example:
$foo = 'C:\\'; # if you had 'C:\' then the ' would instead be escaped +instead of ending the string.
$"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc

Replies are listed 'Best First'.
Re: Re: Problem w/ single-quoted strings
by eweibust (Initiate) on May 14, 2004 at 18:23 UTC

    Very good post. Let me try to break it down.

    Case 1. Simply means you get what's b/t the single-quotes when there aren't any backslashes (complicating things).

    Case 2. Is an exception that allows you to use your termination character inside your string. To do this an "evil" backslash is used. You only get the term char in your string; the backslash disappears....

    Case 3. I guess is a way to get a backslash into your string by using two consecutive backslashes. This confuses me b/c....

    Case 4. Says that a backslash that is followed by anything other then a backslash or term char adds the backslash and the char following it. To me this seems redundant, b/c Case 3 also got you a backslash in your string.

    I am posting some code as an example.
    print 'case 3 ','Test\\ing'; print "\n"; print 'case 4 ','Test\ing'; print "\n"; erikweibust@daleweibust ~/perl/lrn_perl $ ./ case 3 Test\ing case 4 Test\ing

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://353219]
[Corion]: A good morning, choroba!

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2018-06-21 07:43 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.