Embed Perl With Mason -- Read All About It
autarch writes "Embedding Perl in HTML with Mason, written by Ken Williams and me, is now available at booksellers of distinction. Mason is a Perl-based templating system and application framework. The book covers Mason from the basics on up to extending the Mason core with your own subclasses. For more details check out our web site and the O'Reilly site. The latter includes the TOC and a sample chapter."
Amazon has it for $24.47
I just don't understand how code like this (the first example in the Mason Developer Manual) is even remotely readable:
<%perl> /[\s:]/, localtime;
my $noun = 'World';
my @time = split
</%perl>
Hello <% $noun %>,
% if ( $time[3] < 12 ) {
good morning.
% } else {
good afternoon.
% }
I know that Mason has a lot of wonderful things under the hood (the component caching mechanism is pretty swell) but I would rather shoot myself in the head than manage a large website with hundreds of pages that all looked like the above.
I just spent 12 months developing a reasonably complicated website/webapp for my organization, using mod_perl & HTML::Mason. I wholeheartedly recommend Mason for perl-savvy web developers; it does a really nice job of providing powerful tools without creating a steep learning curve. As long as you know perl, of course. Very snappy performance, to boot. I should probably buy a copy of the book as a 'thank-you' to the writers, who, in addition to their substantial coding work, are timely and helpful on the relevant mailing lists.
I've heard about several 'put perl into HTML' tools now like ASP with perl and embperl but I never seem to like it.
I'm now actively involved in WebGUI (a content management system / application server) and we are looking for a templating system to allow for easy costumisation of the display our 'applications'
It just seems a lot more easy to embed HTML into perl than the other way round. Or maybe I should read this book?
I tend to think that Mason is trying to be php with the easy integration of all those nice perl modules. I'm not sure that is the best way.
sig not found
Though it really depends on what kind of system you're trying to make, generally speaking, embedding any serious amount of code into code in a different language often becomes very difficult to maintain. For quick things, it's usually the easiest. For moderately complex things, it's usually a push either way. But for any sizeable website, especially one that is going to be maintained over an extended period of time by multiple people, keeping as much -separate- as possible is a better approach.
.NET, or anything else, you likely will have a lot more going on than simply displaying some simple words, maybe wrapped in some kind of if statement or while loop. You'll need to do some kind of data lookup, perform some kind of transformation of that data, apply a few business rules, then, finally, spit it out in HTML. By far, it is easier to maintain code that isn't mixed with html; like wise, it's easier to maintain html that isn't mixed with code. Plus you then have the option of using that same code to manipulate the data differently, perhaps a GUI application, set of command line utilities, or under a different embedding technology. Decoupling presentation from logic is always a win as complexity increases.
Whether you use ASP, JSP, TT, Mason,
Mason is terrific technology, though, and I'm very glad to see a book dedicated to it finally on the market. It is especially good to finally see some quality documentation on application frameworks that run under mod_perl; before now, there wasn't much besides the excellent wrapmod book and the equally excellent mod_perl cookbook.
True separation of business and display logic.
Do all your processing, calculating, searching, formulating, control flow in perl with no HTML to be seen. Whack all your data in a hash. Pass said hash to Template->process(). Then any [% variable %] text in the HTML looks in the hash. Every web designer worth their salt can deal with that. What is great too is that [% %] comes as ordinary copy in Dreamweaver et alia. They can see where it's going to go. This has its limitations though. Some designers don't grasp the concept of dynamically generated hidden fields to pass variables in a session stack. They tend to omit important tracking stuff.
Also Templates [TT2 being the favourite] will generate your emails. Combined with the rather strenuous Text::Autoformat, you get freakin' nicely formatted text emails.
[% slash_sig_val.text %]
Is the speed there?
:)
Perl is NOT as fast as 'C', but there are quite a fews to make PERL faster...
1. Use MOD_PERL
2. For repetitive tasks, e.g. Email Scanning for SPAM, daemonize your PERL and write a small 'C' client to talk to it.
PERL is an interpreted language, so each time you fire up a script it there is a lot of overhead and PERL has a pretty hefty memory foot print (4-5 MB) on avg per script.
All that being said, remember to use the right tool for the job or the right combinations of tools. IMHO, C and PERL make an unstoppable tag team. PERL is the "swiss army chainsaw" of languages and is great for matching patterns, writing web backends, XML, manipulating data streams, talking to databases, sending email, interacting with LDAP. PERL is my current language of choice, just because I can be very productive, very quickly and there are volumes of programming examples of how to solve a problem at least 20 different ways.
This is probably heresy, but I actually prefer PERL over JAVA,
One other nice thing about PERL is that the backward compatbility is incredible. Recently I had to migrate a bunch of apps from SCO to Linux/Solaris. By merely changing the path to the PERL executable, ALL the PERL scripts ran fine. This was roughly a 5 year jump in time, try that with 'C', Java, or a Microsoft language.
I can't say enough about the power, flexibility, and documentation. If all other programming languages vanished and we were left with 'C/C++' and PERL I think we would survive just fine. Actually, I might need to keep TCL and SQL too
GO PERL!