The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Filter::Indent::HereDoc - allows here documents to be indented within code blocks

SYNOPSIS

  use Filter::Indent::HereDoc;
  {
    {
      print <<EOT;
      Hello, World!
      EOT
    }
  }
  # This will print "Hello, World!" and stop at EOT
  # even though the termination string is indented.

DESCRIPTION

When a 'here document' is used, the document text and the termination string must be flush with the left margin, even if the rest of the code block is indented.

Filter::Indent::HereDoc removes this restriction, and acts in a more DWIM kind of way - that if the terminator string is indented then that level of indent will apply to the whole document.

If there is no terminator string (so the here document stops at the first blank line), then enough whitespace will be stripped out so that the leftmost character of the document will be flush with the left margin, e.g.

  print <<;
       Hello,
      World!
  
  # This will print:
   Hello,
  World!

Changes to terminator strings

In addition to allowing indented here documents, Filter::Indent::HereDoc also provides support for a more permissive style of terminator string, as specified in Perl6 RFC111. The changes are:

  • Whitespace is allowed between '<<' and an unquoted terminator string when the heredoc is defined

  • At the end of the heredoc, whitespace, a single semicolon, and comments are all allowed after the terminator, however other code statements are not

You can force the module to revert to the standard Perl5 style of terminator string by specifying 'strict_terminators' when the module is use'd, e.g.

 use Filter::Indent::HereDoc;
 print << EOT
 Hello, World!
 EOT ;             # this will work
 
 use Filter::Indent::HereDoc 'strict_terminators';
 print << EOT
 Hello, World!
 EOT ;             # this will generate an error

CAVEATS

  • At present, Filter::Indent::HereDoc does not attempt to parse any of the Perl code, it just searches for the '<<' string to locate the start of a here document. Therefore if you need to write '<<' without starting a here document, you must first use no Filter::Indent::HereDoc; to stop the code from being filtered, e.g.

      use Filter::Indent::HereDoc;
      print <<EOT;
      This is a here document
      EOT
      
      no Filter::Indent::HereDoc;
      print "<<EOT";  # This will be printed as normal
  • Due to the way in which whitespace is removed, Filter::Indent::HereDoc can become confused if your editor replaces groups of spaces with tab characters. Different text editors use different tab stops, so for portability reasons it is probably best to avoid this feature.

SEE ALSO

Filter::Simple, http://perl.jonallen.info/modules, perlfaq4, Perl6 RFC111

AUTHOR

Jon Allen, <jj@jonallen.info>

THANKS TO

Michael Schwern for the suggestions about Perl6 RFC111

COPYRIGHT AND LICENSE

Copyright 2003 by Jon Allen

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.