Slashdot Mirror


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."

6 of 37 comments (clear)

  1. Get it cheaper by RedWolves2 · · Score: 3, Informative
  2. Re:Readability? by scrytch · · Score: 3, Informative

    Consider Template Toolkit instead. The above example transliterated looks like:

    [% noun = 'World' %]
    [% PERL %]
    # perl ugliness, but there may be a tt2 split op. note cache means nothing like mason's cache
    @{$cache->{time}} = split /[\s:]/, localtime;
    [% END %]
    Hello $noun,
    [% IF time[3] < 12 %]
    good morning
    [% ELSE %]
    good afternoon
    [% END %]

    Mind you, I'd have put it in variables in another block, so the message would look like:

    Hello $noun, $greeting

    I personally don't use the $foo syntax, and prefer [% foo %] instead, but TMTOWTDI in TT2. TT2's power just blows mason away, and it's not all that difficult to get it doing mason-like persistence things when you combine it with Tie::MLDBM.

    --
    I've finally had it: until slashdot gets article moderation, I am not coming back.
  3. Re:Readability? by autarch · · Score: 2, Informative

    One of the advancements in recent versions of Mason is the ability to (relatively) easily replace parts of the core system with your own modules. So if you loved everything about Mason but the templating language, you could replace the lexer (the class that actually parses components) and easily hook it into the system as a whole.

    For the book, I showed the beginnings of an implementation of a pure XML syntax. I wouldn't actually _use_ such a thing, but the point is that it's now a lot easier to bend Mason to your will.

  4. Re:Readability? by scrytch · · Score: 3, Informative

    > So, what features and/or functionality in TT is so much better than Mason in your opinion?

    The internals of TT2 are amazingly hackable, and provide one of the most useful examples of OOP reuse. It's like a textbook study in design patterns that work. For example, it took me only a few hour's hacking to subclass one class (I forget the name, it's been a while) to create mason-like search behavior for [% INCLUDE %]. Just syntax-wise, TT2 tends to look cleaner -- you don't have to have weird looking noise like <% } > ending all your blocks. You can even change the delimiters from [% foo %], e.g. the metatext %%foo%%, mason's lt;% foo %> or anything else you want, within reason (the parser can get confused).

    Anyway, all those intangibles aside, TT2 is a complete language in its own right -- looks a bit like python, come to think of it -- so you can do even complex logic without embedding perl. When you do embed perl, you can simply 'print' in a perl block, and it will be output to the HTML (I wrote that part, though it's admittedly pretty trivial). In mason, you have to append to a string. If you need to really mess with the internals, you can embed [%RAWPERL%] blocks that are a straight eval. You can enable and disable PERL and RAWPERL blocks from the invocation, a nice way to set policy when using it in mod_perl.

    TT2 has INCLUDE not just for files, but for BLOCK definitions as well. You not only can define blocks, but functions and macros in the TT2 language. You can include arbitrary perl modules and use them in the TT2 language. You can take the output of any block and filter it with user-defined processors using unix pipe syntax. With [%WRAPPER%] you can do a reverse-include, taking the current doc and including it as the value of a magic variable in another document. Variables defined in the TT2 language have lexical scope, and you can choose to include a template in a new scope with [%INCLUDE%] or in the current scope with [%PROCESS%]. TT2 even has some amount of OOP, with [%VIEW%] constructs, which are sort of blocks on steroids. I still haven't completely wrapped my head around views, and they're still kind of primordial at this time, but they seem to be aimed at Mason's strength: components.

    Mason's component model is still superior to TT2, and I was writing a mod_perl system for TT2 that would have addressed that, but I haven't been too active with it lately (read: the year and a half or so). Mostly I've been pining for someone to port TT2 to python, actually. I don't see it as a contest of "Mason sucks, TT2 rules", I just have personal preferences, and would gladly like to see both systems giving each other healthy competition.

    BTW, Slash uses TT2, though not nearly to its full potential.

    --
    I've finally had it: until slashdot gets article moderation, I am not coming back.
  5. Re:Readability? by autarch · · Score: 2, Informative

    Uh, for almost every single thing you mention here there is a Mason equivalent. The sort summary:

    "amazingly hackable" - As of 1.10, it is pretty easy to subclass any one piece of Mason, including the part that implements Mason syntax.

    "complete language" - I don't consider this a desirable feature, so I'm happy to say Mason _doesn't_ match TT in this respect. But this is a question of style and what type of design you prefer, not power. TT is a complete language, but so is Perl, which Mason uses ;)

    Mason components are largely equivalent to defining subroutines/methods. Mason also has its own OO-ish system for defining component methods and attributes.

    With 1.10, we added a "component calls with content" feature that lets you apply filters to arbitrary chunks of content, and 1.14 included a feature for user-defined escaping mechanisms, so that's pretty well covered.

    In the end, I think the one you choose will have much more to do with the particular needs of your project, the people working on it, and which one fits your brain.

    But I would point out that there is really very little that one system does that the other _cannot_ do. Some things are a little easier in one versus the other, but both of them are full-featured enough to make pretty much anything possible.

  6. Re:Readability? by mobosplash · · Score: 2, Informative

    I use Mason and Perl every day, and I'm not sure where the confusion is. Is it just you don't understand Perl, or that you haven't used mixing a programming language with HTML? If the latter, I have to tell you that this is the ONLY way to go when you have complex web pages.


    Mixing a programming language with your html template is not the only way to go. Tapestry (http://tapestry.sourceforge.net/) and XMLC (http://xmlc.enhydra.org/) put no programming code in the html and Tiles (http://jakarta.apache.org/struts/userGuide/dev_ti les.html) uses a tagging system that is very html like. These are all Java based solutions.

    I've done a lot of the mixing approach, especially with PHP, but now I use Tiles and it is incredibly powerful and flexible.

    Templating approaches like Velocity and patTemplate (for PHP) have small amounts of code in the html but as sub language designed for more readability so they are easier to maintain than the above.