Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Template::Toolkit - catching errors when plugin is missing?

by Skeeve (Parson)
on Jul 04, 2014 at 08:11 UTC ( [id://1092261]=perlquestion: print w/replies, xml ) Need Help??

Skeeve has asked for the wisdom of the Perl Monks concerning the following question:

I just had report of a user of my Dancer app because it crashed due to a missing module.

While it was my mistake that the debug code was still in my Template::Toolkit template, I have to admit that I did not put the module on the customer's machine on purpose.

This is the code which failed:

<!--[% USE Stash; USE Dumper Indent = 1; Dumper.dump( Stash.stash() ) %]-->
which I included in some templates to be able to see whether or not all my variables "arrived" at the template.

Since the "Stash" module is not on the customer's machine, the template failed.

Now I'm wondering if it's possible to instruct the template to silently do nothing when the module is missing.


s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Replies are listed 'Best First'.
Re: Template::Toolkit - catching errors when plugin is missing?
by DrHyde (Prior) on Jul 04, 2014 at 12:17 UTC
    TT has TRY and CATCH keywords. Perhaps they'll help.

      Indeed they did!

      <!--[% TRY; USE Stash; USE Dumper Indent = 1; Dumper.dump( Stash.stash() ) | replace('-(?=-)', '- '); CATCH %] No dump without stash ERROR! Type: [% error.type %] Info: [% error.info %] [% END %]-->

      That's my new DUMP.tt


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: Template::Toolkit - catching errors when plugin is missing?
by SimonPratt (Friar) on Jul 04, 2014 at 10:26 UTC

    You should be able to pop the offending code into a BEGIN block, replacing use with require and wrap with eval to catch any failures:

    BEGIN { eval { require Stash; require Dumper; $Dumper::Indent = 1; Dumper.dump( Stash.stash() ) }; }

    Another thing I tend to do with debug code is set a constant (such as DEBUG) and make sure all of my debug code is wrapped up with if DEBUG statements, which completely strips the code out when not running in debug mode

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1092261]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-20 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found