Sometimes, blocks of code need to be quoted for later evaluation. One could use a quote operator, or a here-doc. In the past, I've claimed that here-docs make things easier to escape. Here's the basis for my claim, complete with (simplified) realistic examples.

Single-Quote

When using here-docs, extra slashes are never necessary. You could write your code exactly as if it wasn't in a here-doc at all.

When using the quote operator, backslashes must be doubled when they occur in front of another backslash and when they occur in front of the string delimiter.

Obviously, a backslash must be added before an unbalanced delimiter when using the quote operator. However, unbalanced delimiters are rare, and omitting the slash will likely cause an (easy to detect) compile-time error.

$hd = <<'__EOS__'; '\\\\' =~ /^..$/ __EOS__ $q = q[ '\\\\' =~ /^..$/ ]; $q2 = q[ '\\\\\\\\' =~ /^..$/ ]; print($hd); print(eval($hd) ?1:0, "\n"); print($q ); print(eval($q ) ?1:0, "\n"); print($q2); print(eval($q2) ?1:0, "\n"); $hd = <<'__EOS__'; '[]' =~ /\[\]/ __EOS__ $q = q[ '[]' =~ /\[\]/ ]; $q2 = q[ '[]' =~ /\\[\\]/ ]; print($hd); print(eval($hd) ?1:0, "\n"); print($q ); print(eval($q ) ?1:0, "\n"); print($q2); print(eval($q2) ?1:0, "\n"); $hd = <<'__EOS__'; 'funny' # :-] __EOS__ # Syntax error # ------------ # $q = q[ # 'funny' # :-] # ]; $q2 = q[ 'funny' # :-\] ]; print($hd); print(eval($hd) ?1:0, "\n"); print($q2); print(eval($q2) ?1:0, "\n");

Double-Quote

When interpolation is necessary, there's no difference between here-docs and the quote operator.

Obviously, a backslash must be added before an unbalanced delimiter when using the quote operator. However, unbalanced delimiters are rare, and omitting the slash will likely cause an (easy to detect) compile-time error.

$hd = <<"__EOS__"; '\\\\\\\\' =~ /^..\$/ __EOS__ $qq = qq[ '\\\\\\\\' =~ /^..\$/ ]; print($hd); print(eval($hd) ?1:0, "\n"); print($qq); print(eval($qq) ?1:0, "\n"); $hd = <<"__EOS__"; '[]' =~ /\\[\\]/ __EOS__ $qq = qq[ '[]' =~ /\\[\\]/ ]; print($hd); print(eval($hd) ?1:0, "\n"); print($qq); print(eval($qq) ?1:0, "\n"); $hd = <<"__EOS__"; 'funny' # :-] __EOS__ # Syntax error # ------------ # $qq = qq[ # 'funny' # :-] # ]; $qq2 = qq[ 'funny' # :-\] ]; print($hd ); print(eval($hd ) ?1:0, "\n"); print($qq2); print(eval($qq2) ?1:0, "\n");

In reply to here-docs vs quote operators by ikegami

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.