PBP: 044 Heredoc Quoters

The PBP suggests that all heredocs be explicitly and deliberately quoted.  When I first read this, I didn’t know you could do that!  It’s a great idea.

What this means is to put the right kind of quotes around the name of the marker when it is used.  This tells Perl which kind of quoting, interpolating or not, it should be using for that heredoc:


my $thing = << 'END_THING';

This is something, all right!

It will have $this and $that in it, not blanks and missing variables.

END_THING

&nbsp;

my $msg = << "END_MESSAGE";

This message contains the whole thing:

$thing

END_MESSAGE

The heredoc assigned to $thing is not interpolated.

The heredoc assigned to $msg is.

And, because they’re quoted, you can tell right away by looking.

Pop quiz:  Which is the default, interpolated or not?

Answer: Hell if I know, I always quote them explicitly.

 

Leave a Reply