10.07.2015 Views

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

254CHAPTER 12 ■ THE TEMPLATE TOOLKIT[% CATCH %]Error: [% error.type %]: [% error.info %][% END %]Along with some leading and trailing blank lines (not shown), the output from this exampleis as follows:This code doesn't do much except maybe fail.Error: file: sometemplatefile: not foundAs you can see in the preceding example, a special object called error is created withinthe CATCH block. This error object has two properties, type and info, both of which are printed.You can use the error type <strong>to</strong> perform more granular exception handling. Listing 12-7 showsthe file type error being caught.Listing 12-7. Adding the Error Type <strong>to</strong> the CATCH Block[% TRY %]This code doesn't do much except maybe fail.[% CALL othercode %][% INCLUDE sometemplatefile %][% CATCH file %]File Error: [% error.info %][% CATCH %]Error: [% error.type %]: [% error.info %][% END %]The output from Listing 12-7 is as follows:This code doesn't do much except maybe fail.File Error: sometemplatefile: not foundThe code sample used two new items. First, the type of error was checked and caught if itwas a file type (as would be reported by error.type), and a default CATCH block was also created.One additional feature of the TRY-CATCH syntax is a FINAL block. Content and code withina FINAL block get processed regardless of what happens within the TRY-CATCH blocks. This canbe helpful for printing footer or other information that’s necessary for the template you’re processing.Blocks of <strong>Perl</strong>As if all of the power of template processing with Template Toolkit shown already isn’t enough,you can also execute blocks of pure <strong>Perl</strong> code within a template by defining the code withina PERL block inside of the template. The following example shows a <strong>Perl</strong> block in action:[% PERL -%]print "Hello, this is <strong>Perl</strong>!\n";[% END -%]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!