Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by Skeeve (Vicar)
on Jul 04, 2014 at 08:11 UTC ( #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
Node Status?
node history
Node Type: perlquestion [id://1092261]
Front-paged by Arunbear
help
Chatterbox?
marto should develop the 'anti-social network'
[marto]: sign up to make it impossible to be contacted online :P
[Discipulus]: karl: configuring the network is like: nmcli conn add con-name "name name" ifname eth0 type eterneth.... not so different from powersehell longness

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2017-12-14 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (389 votes). Check out past polls.

    Notices?