Hello again monks,

For a long time I thought single quotes took a string exactly as it was and ignored all escaping. Faster, better for hardcoded values, etc. It made sense! But when putting Windows paths into strings I noticed some odd behaviors. Usually I just chalk this up to an eccentricity of Perl and just workaround it but I figured the time has come to clarify. Better explained through code:

$foo = '\\some.address.or.whatever\subdir\'; print $foo;

And even my IDE's syntax highlighting picks this up - I've escaped that last single quote which makes sense, I guess. So this code creates a runaway string - but I thought single quotes don't do handle any escaped characters? Hm. Except the single quote. Fair enough, how else would you get a single quote character into a singly quoted string?

But then this, when I need double backslashes to indicate a network path:

$foo = '\\some.address.or.whatever\subdir\file'; print $foo;

Okay, so I didn't try to end on a backslash like last time so at least this snippit executes. But you'll notice that the double backslash gets escaped to a single backslash. Arg, that won't do. So usually I'll just go in and drop the extra backslashes in. But let's be real here - '\\\\' just looks godawful.

First I went to q{string_w_escapes}, but that does the same thing (understandably). But I was really hoping it would resolve that trialing backslash issue - which it doesn't. It just escapes the delimited character, so now I find that some non-alphanumeric escaped characters (ex: \{) are parsed in singly quoted strings? Double arg.

Then I remembered that Perl allows explicit string building using <<'here-doc'. I hate using heredocs - they're ugly and feel clunky. But let's give that a shot:

$foo = <<'PATH'; \\some.address.or.whatever\subdir\ PATH print "this is a test${foo}again\ntesting";

Okay, so I've single quoted the heredoc to skip any processing of escapes - which finally works. Hooray, look at those two backslashes! But wait, hold the phones... now there is this unavoidable newline character at the end of the string. Triple agh! Probably something to do with the nature of how heredoc tags are parsed. Sure wish it would just terminate the string with a nice "\0". Since I do not think it's possible specify a heredoc without that trailing newline I guess this option doesn't really work either (almost relieved, I would hate to define all my hardcoded paths as heredocs).

I just want the string as I've typed it, no matter what character(s) it contains. Seems like it could be useful if one had to define a large number of strictly typed strings: reduces resource overhead of processing each of them for escapes and whatnot. Or is this just the nature of Perl encoding all strings into its internal representation 'under the hood' and I'm going to have to live with it? I really thought there would be some builtin like ql{} (quote literal?) which would allow this kind string definition.

TLDR:
So is there a (simple) way to put a string into a scalar variable without any character escaping or processing of the contents of that string at all?


In reply to Single Quotes - how to avoid any escape processing? by temporal

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.