This days I had an idea, another since this is my work, have ideas to make the development of our systems faster and with less developers and experts. Don't say to use this or that, or that I'm wasting my time, they pay me very well to do that, ok?!

Well, here we have a envirionment that embeds Perl into HTML, yes another of this kind. It's similar to PHP, but the code is Perl, and have a lot of resources to work with OO entities, portable DB, etc... Well, it have all that we need to make portals fast.

Now I'm working to extend the Perl syntax, so, I made some simple analysis, and saw that the more common commands are:

print "<b>some quoted HTML<b>" ; ## and my $html = "<i>assign some html</i>" ;
But we know that when we declare a HTML with quotes is just sux! For example, when we want to declare with multiple lines, or to declare quotes and variables inside the quoted string:
print qq` <font color="#000000"> multiple lines and quotes </font> ` ;
Well, the syntax above is still simple, but remember that this is something that we write all the time! So, a long time ago we have added HTML blocks to the template system:
<html> <% print <% html_foo>(@args) ; %> <% html_foo(@args) <font color="#000000"> Args: @args </font> %> </html>
But I still see some developers declaring a lot of qq``! Why that? Because they don't want to create a new HTML BLOCK in the template just to print simple things. They think that HTML BLOCKS are very useful, but for blocks that will be used and called in many places.

So, I had some idea in the last week. Why not use some delimiter to declare a full line as a quoted string for output or assigment, making something similar to Perl comments:

>> this is an output! ## similar to: print "this is an output!\n" ;
And the advantage is that is not needed to open and close quotes, or to care about the use of quotes inside it, what is perfect for HTML data:
<table border="0"> <% for ( @names ) { >> <tr> >> <td color="#333333">$_</td> >> </tr> } %> </table>
Than I have added the same idea for assigment:
<% for ( @names ) { $html .= << <tr> . << <td color="#333333">$_</td> . << </tr> } %>
Well, what I really want is to know what you think about this extensions. I already have implemented basically the new syntax into our template system, just to see it working. I also think that this syntax can be helpfull to make a code cleanner, what will make us to win time on maintenance of this codes.

Thanks in advance!

Graciliano M. P.
"Creativity is the expression of the liberty".

Replies are listed 'Best First'.
Re: Extending the Perl Syntax on PERL + HTML envirionments.
by davorg (Chancellor) on Aug 05, 2004 at 10:39 UTC

    It seems that everyone has a need to invent their own templating system in Perl. The clever ones quickly realise that they are wasting their time and that there are already a huge number of templating systems to choose from and there is rarely a good reason to add to that number.

    If you like Perl in your HTML, then why not look at Mason, Embperl or even Text::Template. Personally I prefer keeping Perl out of my HTML and, instead, using the simplified markup language of the Template Toolkit.

    I know it seems like a big leap to switch from your home-grown solution to a new one, but it's really worth making the change. You'll get a far more powerful system that is being used (and therefore tested) by hundreds or thousands of other people. It also means that there is a huge community of potential new employees who already know a lot about your templating system.

    I know you asked people not to suggest that you use something else but, to be honest, I really think that's the best advice in this case.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Extending the Perl Syntax on PERL + HTML envirionments.
by Arunbear (Prior) on Aug 05, 2004 at 10:28 UTC
    I think that ASP / PHP way of embedding code in HTML using '<% %>' is butt ugly, even more so when you need to use if / else branching constructs:
    <% if($some_condition) { %> <b>Some text</b> <% } elsif($some_other_condition) { %> <b>Some other text</b> <% } else { %> <b>Yet more text</b> <% } %>
    Yuck! (Don't your eyes just glaze over when reading that?) Is that what branching looks like in your templating system?

    The Mason way of embedding is simpler and more economical.
    Contrast
    <table border="0"> <% for ( @names ) { >> <tr> >> <td color="#333333">$_</td> >> </tr> } %> </table>
    with
    <table border="0"> % for ( @names ) { <tr> <td color="#333333"><% $_ %></td> </tr> % } </table> <%doc> This is table.mas </%doc> ----------------------------------- # now to assign this HTML to a variable: my $html = $m->scomp('table.mas');

    If you want to re-invent wheels, why not re-invent one that hasn't been re-invented yet (in Perl)? :-) Be well.
Re: Extending the Perl Syntax on PERL + HTML envirionments.
by BUU (Prior) on Aug 05, 2004 at 07:11 UTC
    Aside from the fact that its ugly, unintuitive and breaks perl builtins, why not just steal even more from php and simliar things and allow flow control to wrap html? Something like:
    <html> <body> <table> <% for( @loop ) { %> <tr><td><% $_ %></td></tr> <% } %> </table> </body> </html>
    (Or of course you could use of of the 32 million prebuilt wheels. Of just tell them to use print already. )
      This kinde of code already exists in the syntax of the template system. But when the developer is inside a CODE BLOCK and want to print some HTML data, the use of quoted strings is not so goo, this is why I'm thinking in this extension of the syntax.

      Graciliano M. P.
      "Creativity is the expression of the liberty".

Re: Extending the Perl Syntax on PERL + HTML envirionments.
by Aristotle (Chancellor) on Aug 05, 2004 at 21:51 UTC

    That looks like an ugly way to do heredocs. If you want to meddle with the syntax, do what Perl6 does and allow heredocs and their delimiters to be indented. No need to add new syntax when there's existing one that could be extended to do what you want in a cleaner fashion.

    But anyway, you should be using an existing templating system.

    Makeshifts last the longest.

      It is different from heredocs in that it gets rid of the end marker, and it is better suited for medium lenght strings. For me it is competing with the qq rather then with heredocs.
Re: Extending the Perl Syntax on PERL + HTML envirionments.
by zby (Vicar) on Aug 05, 2004 at 07:06 UTC
    I am not so sure about the HTML templating part, as there allready is an aboundance of HTML templating libraries, but the new quoting operator seems nice. For me it is much simpler than using the qq with a choosen character and then remembering to not use that particular character in the string. It is much simpler to read as well, you know from the beginning where the string ends.