[Templates] Return of the error() method

Andy Wardley abw@cre.canon.co.uk
Tue, 15 Aug 2000 16:07:22 +0100


On Aug 15,  4:55pm, Jonas Liljegren wrote:
> I found one instance there my new $template returned a Template::Exception
> from a call to $template->error() and another there it returned the
> Template::Exception as_string().

If the template that you name in the call to process() doesn't
exist then you get a simple string returned.  This should really
be an exception (i.e. it's a bug which will get fixed RSN)

    $template->process('nofile')
	|| die $template->error();    # returns "nofile: not found"

But everything else *should* be passed back as an exception object.
The only thing to watch out for is that the stringification operator
is autoloaded for Template::Exception so if you print it, or treat
it in any way as a string, then the as_string method gets called
automatically.

    $template->process('okfile')      # but something throws an error
         || die $template->error();

Thus, in the above example, an exception gets returned but die
stringifies it, and the as_string method gets called automatically.

But you can also do this:

    unless ($template->process($myfile)) {
	$error = $template->error();    # Template::Exception object
        if ($error->type eq 'whatever') {
            print $error->info, "\n";
            ...
        }
    }

> I also noticed that there is a third element in the Exception object,
> quoting a the part of a parsing error.

Yes, that's officially undocumented (but probably should be documented
for developers).  It contains the processed contents of the current
block up to the part where the exception occurred.

> I would like to get my hands on the Template::Exception object from a
> failed $template->process(), in order to get to the exception type.

Apart from that one case outlined above, you can simply call the error()
method and get an object returned.

> And I would also like to access that quoted template part.  It does not
> have a documented get method.

    $error->text();


HTH
A

-- 
Andy Wardley <abw@kfs.org>   Signature regenerating.  Please remain seated.
     <abw@cre.canon.co.uk>   For a good time: http://www.kfs.org/~abw/