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

10 of 37 comments (clear)

  1. Get it cheaper by RedWolves2 · · Score: 3, Informative
    1. Re:Get it cheaper by Monkelectric · · Score: 3, Insightful

      Dosen't the url he gave have a referal code?

      --

      Religion is a gateway psychosis. -- Dave Foley

  2. Readability? by glenstar · · Score: 4, Insightful

    I just don't understand how code like this (the first example in the Mason Developer Manual) is even remotely readable:

    <%perl>
    my $noun = 'World';
    my @time = split /[\s:]/, localtime;
    </%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.

    1. 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.
    2. Re:Readability? by Reality+Master+101 · · Score: 3, Insightful

      To be honest with you, other than perhaps understanding the regular expression, what's so difficult about it?

      The <%perl> section is perl code. Two variable assignments. @time is an array with the elements of the current time. <% %> encloses an expression, which is inserted into the HTML. % in the first column is Perl code, which compares the third element of the time array (which is the hour) to 12. The 'if' statement encloses two pieces of HTML.

      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.

      Aside: the regular expression is actually kind of stupid, because you can just do "my @time = localtime" and the third element is the hour.

      --
      Sometimes it's best to just let stupid people be stupid.
    3. Re:Readability? by glenstar · · Score: 3, Interesting
      It's not confusion so much as just how, um... unpleasant the code looks. Using code delimeters like <% is bad enough, but with Perl you end up with lots of $, %, etc... to *me* very, very unreadable code. So, to answer your question: I am not a Perl fan. At all. That being said, I prefer templating languages that allow for very clean separation of logic and HTML (as one example, look at Skunkweb, skunkweb.sourceforge.net)... then again, I am a Python bigot.

      Just my opinion. Like I said in my original post, I think that the concepts behind Mason are very valid and well implemented, I just dislike the templating language.

    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.
  3. Not a paid endorsement by jslag · · Score: 3, Interesting

    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.

  4. Embedding language X in Other Language Y by turnerjh · · Score: 5, Insightful

    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.

    Whether you use ASP, JSP, TT, Mason, .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.

    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.

  5. Template Toolkit by tezza · · Score: 3, Interesting
    I don't use Mason. I use Template Tookit.

    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 %]