I leanred something very interesting about HTML::Mason: it can stream the HTML output in phases as opposed to building an entire HTML document and then delivering it.

When a ton of HTML/XML data is being delivered, this streaming facility becomes very important: if you are sending huge files, you cannot afford to build the entire thing in memory . You have to bust out one part dynamically, kick it over, reclaim the memory and keeping going.

  • Comment on HTML::Mason can stream an HTML/XML document!

Replies are listed 'Best First'.
Re: HTML::Mason can stream an HTML/XML document!
by dragonchild (Archbishop) on Apr 09, 2005 at 00:14 UTC
    HTML::Template has a documented print_to option for the output() method that streams exactly like you're describing. And, HTML::Template is poorly named - I use it for a lot more than just HTML. If I was outputting XML, I would use and have used H::T.
      but dont you have to build up the entire data structure before passing it to HTML::Template? I mean, you can't pass an HTML::Template template a DBI statement handle and call fetchrow within the template can you?

      Perhaps the add-on expr module allows this? A cursory glance through the docs didn't show any such capability.

        You generally will build the entire data structure ahead of time, but you don't have to. Another documented option - the associate parameter to the new() method, allows you to associate any object that provides a param() method to the given template. Then, what HTML::Template will do when it needs a parameter (for a TMPL_VAR, TMPL_LOOP, or TMPL_IF/UNLESS), is check the following places in order:
        1. Its parameter stash
        2. The associated object(s), in the order they were associated in
        3. If within a loop and GLOBAL_VARS has been specified, it will check the next outermost scope, repeating the same actions
        The param() method is expected to return undef if it doesn't handle that parameter name. (Make sure you specify the case_sensitive option or be case-insensitive yourself.)

        So, you can now do something like:

        package My::Associated::DBH::Wrapper; # Called as: # my $thingy = My::Associated::DBH::Wrapper->new( # sth => $sth, # param_name => 'Foo', # ); sub new { my $class = shift; my %args = @_; bless \%args, $class; } sub param { my $self = shift; my ($param) = @_; return unless $param eq $self->{param_name}; my @values = $self->{sth}->fetchrow_array; return unless @values; return $values[0]; }
        Then, when you build your template, you can go ahead and do this:
        my $template = HTML::Template->new( filename => 'my_template.tmpl', associate => [ $thingy, $query ], );
Re: HTML::Mason can stream an HTML/XML document!
by webfiend (Vicar) on Apr 08, 2005 at 19:58 UTC

    I don't like Mason much either, but that's definitely an interesting interview approach you've applied there ;-)

    The ability to stream output is very handy indeed, since it allows the important bits of a page to display even if a component breaks along the way. Well, depending on how you've got error handling set up, but you get the idea. There are plenty of little things like that which make Mason a good deal more pleasant to use. Then again, can't you do the same thing from regular CGI by setting $| to a true value? After all, your application is just sending to STDOUT.

      Yes, you can do that with a regular CGI when generting output directly. The key problem, though, is that most templating languages are infeasible to stream. They just don't have the semantics for it.

      "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: HTML::Mason can stream an HTML/XML document!
by Arunbear (Prior) on Apr 09, 2005 at 20:44 UTC
    Some example code to illustrate your point would be welcome.