Hi Mark --

> I was just working with CGI::App today and ran into a situation I've
> run into several times before: I got an internal server error 
> because a
> template could not be found when load_tmpl() was called. It would be
> nice if CGI::App could systematically address this issue by providing
> some error handling for this case. Here's a specific proposal to do
> that:


The problem you describe is a classic one of exception handling.  Perl is
notoriously bad at exception handling.  Since there is no universal
"exception handling system" in Perl, many people have tried to roll their
own.  I would prefer not to go down that path.

In lieu of rolling an exception-handling system, I have tried to implement
the behavior which is valid for most people most of the time.  Most of the
time, failure to locate a template is a fatal error.  Templates are,
generally, statically assigned.  If a programmer types "serch_form.tmpl"
instead of "search_form.tmpl", it seems sensible to die().  The programmer
has made a typo, and the program has a bug as a result.

If, OTOH, you have devised a system where users specify templates (via a
form text field, for instance), it will not do to die().  Clearly, you need
some other type of handling for this exception.  Fortunately, the
object-oriented nature of CGI::Application makes it easy for you to roll
your own.  By simply overriding the load_tmpl() method, you can do whatever
you like:


  package MyCGIApp;
  use base q/CGI::Application/;
  sub load_tmpl {
        my $self = shift;
        my ($tmpl_file, @extra_params) = @_;

        my $t;
        eval {
                require HTML::Template;
                $t = HTML::Template->new_file($fq_tmpl_file, @extra_params);
        };

        if ($@) {
                # We got an error!
                $self->param('TMPL_ERROR' => $@);
                return undef;
        } else {
                # Everything is OK!
                $self->param('TMPL_ERROR' => '');
                return $t;
        }
  }


FWIW, it is not CGI::Application which is die()-ing when you specify a bad
template -- it is HTML::Template.  If Perl had an exception handling system,
HTML::Template would throw a "not found" error, and your app could catch it.
As it is, you have to just live with it, or catch it in an eval block, as I
have illustrated.


TTYL,

-Jesse-


----

  Jesse Erlbaum, CTO
  Vanguard Media
  212.242.5317 x115
  [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to