SPUG: Perl and HTML

Showell30 at aol.com Showell30 at aol.com
Tue Oct 24 10:59:17 CDT 2000


Since Jason has opened up the topic of Perl and HTML, I will throw my two 
cents in.  

I am working on a project where I have a bunch of web pages with 
family/vacation photos that I need to tie together, giving them common 
look-and-feel, common table of contents, etc.

I considered a template approach, and decided against it for a a couple 
reasons:

1) If your template solution does not have an "include" feature, it can be 
difficult to reuse HTML.
2) If you do use the "include" feature, you can quickly have a bunch of files 
hanging around, which gets to be a mess after a while.  
3) I've done template solutions before that were ugly (non-Perl, though).
4) I think of data first, presentation second, so templates just seemed wrong 
for my coding psychology.
5) This is a personal project, so I can afford to be lazy and wrong about it. 
;)

Anyway, I decided to use HTML::Subs and HTML::Entities instead.  I have found 
those to be very smooth sailing.  Here is what I like:

1) Code is prettier -- no matching of start and end tags (although some would 
argue matching parens is just as bad).
2) Reuse scheme is more Perlish to me.  If I have a cool way of laying out a 
table, I share it by making a subroutine to return the table widget, whereas 
a template solution might lend itself to having an include file.
3) My approach does not preclude templates in the future.  I do 90% of my 
HTML through Perl subroutines, but sometimes it's easier just to code up a 
little raw HTML, and I either do that as a here document, or I slurp it in 
from a file, and then I use HTML::TreeBuilder to stick it into my 
HTML::Entities tree.  

Having said all this, I must say that a friend of mine has done a very 
similar project w/posting photos to the web, and he went the template 
approach.  He went with (gasp) ASP, and I must say, his code looks pretty 
clean and maintainable.  Also, at the last SPUG meeting, the slashcode guy 
says that they have been happy with the template solution.  Even so, I am 
content with my non-templates solution.

If you do go the Subs/Entities route, here are some headaches that you might 
encounter:

1) Tr for table rows can trip you up, if you don't realize it's a reserved 
word in Perl.  This is well documented, but I got tripped up on it any way.
2) It seems hard to get &nbsp's into the page, without them being 
overtranslated.  The workaround is easy, though.
3) Mixing raw HTML into the Subs/Entities data structure was tough for me at 
first.  In the end, I wrote like a three line function that used TreeBuilder 
to solve the problem, but I wish this had been documented better, or if there 
is an even simpler way, I wish it were documented more clearly.

One last thought for Jason -- check out the Data::Locations module.  Even if 
you don't use it for HTML, it's great food for thought. 

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list