modperl_perl_html_indenting_minutia

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



To: mod_perl <modperl@apache.org>
From: Andrew Ho <andrew@tellme.com>
Subject: [OT] Here document indenting (Was: XMas printing
benchmark)
Date: Fri, 29 Dec 2000 11:15:57 -0800 (PST)

Hello,

DC>    (my $text =<<'    foo') =~ s/^\s+://mg;
DC>        :<h1>Hello, World!</h1>
DC>        :  <p><a href="http://foo.org/">I</a> am an indented link.</p>
DC>        :      <p>So am <a href="http://bar.org/">I</a>.</p>
DC>    foo
DC>    print $text;

This, and other methods (without the beginning colon, for example) are
discussed in recipe 1.11 of the Perl Cookbook.

If you just want to indent your code, and don't care about the indentation
of the HTML, you can do something less expensive (let's pretend the above
was in a loop, or had interpolated variables, in which case the regex
could be potentially expensive):

    print<<"    EndHTML";
        <p>This HTML code will appear indented in the output.
         But, the ending delimiter can also be delimited, yay.</p>
    EndHTML

The only bad thing is that you have to count spaces carefully. Also if you
DO indent your HTML (pretty rare as I've observed in dynamically generated
pages that aren't templatized), you get slightly ugly code like this:

    print<<"    EndHTML";
  <p>The HTML I'm outputting dictates that this &lt;p&gt; appears at
   the indent level shown here. This makes my code ugly.</p>
    EndHTML

That's still better IMO than this:

    print<<EndHTML;
  <p>The HTML I'm outputting dictates that this &lt;p&gt; appears at
   the indent level shown here. This makes my code ugly.</p>
EndHTML

Because the latter totally destroys your identation.

FWIW, Ruby (http://www.ruby-lang.org/) has a nice abbreviation for the
indented delimiter--if you use an end token with a prepended hyphen, it
lets you indent the end token. Example:

    print <<-EndHTML
        <p>This HTML code will appear indented in the output.
         In Ruby, this is slightly neater than the equivalent
         Perl syntax.</p>
    EndHTML

This would be a great feature to port to Perl; it eliminates the tedious
"how many spaces did I indent?" problem which results in a "Can't find
string terminator..." error. Oddly enough, Ruby will complain, though, if
you omit the space between the print and the <<.

===


the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu