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");

Replies are listed 'Best First'.
Re: here-docs vs quote operators
by brian_d_foy (Abbot) on Feb 16, 2006 at 21:33 UTC

    Although you're right about the quoting mechanisms, I wouldn't tell people to follow your examples. If there's a way not to use the string form of eval, take it. Creating an anonymous subroutine lets you write Perl code exactly how you should write it and without any worries about quoting. You don't have to worry about the difference in any of these.

    my $hd = sub { '[]' =~ /\\[\\]/ };

    In your last example, since you're already using an alternate delimiter, just choose one that doesn't show up in the data. That takes care of the syntax error quite nicely, and it's that reason we have alternate delimiters. Then you won't need three lines for a one line statement. :)

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
      I wholely agree. However, sub cannot always be used. My typical use for here-docs is when using Parse::RecDescent, whose input includes uncompiled code.
      Next, print that anonymous sub to stdout.

      If you can do it, tell me how.

        From Perl Hacks:

        my $deparse = B::Deparse->new(); print $deparse->coderef2text( $anonysub );

        Use Data::Dump::Streamer to do the deparsing for you. (This is also mentioned in perl-hacks I think :-)

        use Data::Dump::Streamer; my $hd = sub { '[]' =~ /\\[\\]/ }; Dump($hd)->Names('hd')->Out(); __END__ $hd = sub { '[]' =~ /\\[\\]/; };
        ---
        $world=~s/war/peace/g

        How about this?

        use strict; use Data::Dumper; sub print_code { my ($sub) = @_; my $d = Data::Dumper->new( [$sub] ); $d->Deparse(1); print $d->Dump(); }; print_code(sub{ print 'This is a coderef' });