1Text::Xslate::Manual::DUesbeurggCionngt(r3ipbmu)ted PerlTeDxotc:u:mXesnltaattei:o:nManual::Debugging(3pm)
2
3
4
6 Text::Xslate::Manual::Debugging - Debugging techniques for Xslate
7 templates
8
10 This document describes techniques for debugging templates.
11
12 Setting "verbose => 2"
13 Try "verbose => 2" in the first step. This option enables full
14 warnings, especially warnings related to "undef".
15
16 File names and line numbers
17 Xslate messages include file names, line numbers, and, if possible,
18 source code lines which seems problems.
19
20 You can also access the file name and the line number in templates by
21 "__FILE__" and "__LINE__" tokens just like as Perl.
22
23 If you want reports files and lines from your registered functions,
24 "Text::Xslate->current_file" and "Text::Xslate->current_line" in
25 callbacks are the same as "__FILE__" and "__LINE__" in templates
26 respectively.
27
28 sub my_sqrt {
29 my($n) = @_;
30
31 if($n < 1) {
32 # return a message instead of warnings
33 return sprintf "!!! Can't take sqrt of $n at %s line %d !!!",
34 Text::Xslate->current_file, Text::Xslate->current_line;
35 }
36 return sqrt($n);
37 }
38
39 my $tx = Text::Xslate->new(
40 function => { sqrt => \&my_sqrt },
41 );
42
43 To dump values
44 You can use any dumping modules via the "function" option, but Xslate
45 has a builtin "dump" filter to dump template values.
46
47 <: $value | dump # Dump $value with Data::Dumper :>
48
49 Detection of missing variables (or typos or variable names)
50 Xslate itself has warning system for use of uninitialized values, but
51 sometimes it is not enough.
52
53 If you want fill in some string, e.g. FILL ME, for missing variables,
54 you can use the hash_with_default() utility. For example:
55
56 use Text::Xslate::Util qw(hash_with_default);
57 $tx->render($name, hash_with_default(\%vars, sub { "FILL ME '@_' " }) );
58
59 Note that this is really slow because it is a tied-hash wrapper.
60
61 Customization of error messages
62 You can customize error handlers by "warn_handler" and "die_handler".
63 In these handlers, you can call "Text::Xslate->print()" method in order
64 to add your custom messages to the output buffer, which makes debugging
65 easier.
66
67 #!perl -w
68 use strict;
69 use Text::Xslate;
70 my %vpath = (
71 hello => 'Hello, <: $lang :> world!' . "\n",
72 );
73 my $tx = Text::Xslate->new(
74 path => \%vpath,
75 verbose => 2,
76 warn_handler => sub { Text::Xslate->print('[[', @_, ']]') },
77 );
78
79 print $tx->render('hello', { });
80 # => Hello, [[use nil to print at ...]] world!
81
83 Text::Xslate
84
85 Text::Xslate::Manual
86
87
88
89perl v5.38.0 2023-07-2T1ext::Xslate::Manual::Debugging(3pm)