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. 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 Twirlip+of+the+Mists · · Score: 2, Insightful

      That code is virtually identical, except for some intricacies of syntax, to PHP or JSP.

      All embedded scripting code looks pretty much the same. Mason is no more or less readable than any of the others.

      --

      I write in my journal
    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.
  2. Better to embed HTML into perl? by synq · · Score: 2, Insightful

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

  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.