Commit | Line | Data |
d442cc9f |
1 | =head1 NAME |
2 | |
3ab6187c |
3 | Catalyst::Manual::Tutorial::07_Debugging - Catalyst Tutorial - Chapter 7: Debugging |
3533daff |
4 | |
d442cc9f |
5 | |
6 | =head1 OVERVIEW |
7 | |
4b4d3884 |
8 | This is B<Chapter 7 of 10> for the Catalyst tutorial. |
d442cc9f |
9 | |
10 | L<Tutorial Overview|Catalyst::Manual::Tutorial> |
11 | |
12 | =over 4 |
13 | |
14 | =item 1 |
15 | |
3ab6187c |
16 | L<Introduction|Catalyst::Manual::Tutorial::01_Intro> |
d442cc9f |
17 | |
18 | =item 2 |
19 | |
3ab6187c |
20 | L<Catalyst Basics|Catalyst::Manual::Tutorial::02_CatalystBasics> |
d442cc9f |
21 | |
22 | =item 3 |
23 | |
3ab6187c |
24 | L<More Catalyst Basics|Catalyst::Manual::Tutorial::03_MoreCatalystBasics> |
d442cc9f |
25 | |
26 | =item 4 |
27 | |
3ab6187c |
28 | L<Basic CRUD|Catalyst::Manual::Tutorial::04_BasicCRUD> |
d442cc9f |
29 | |
30 | =item 5 |
31 | |
3ab6187c |
32 | L<Authentication|Catalyst::Manual::Tutorial::05_Authentication> |
d442cc9f |
33 | |
34 | =item 6 |
35 | |
3ab6187c |
36 | L<Authorization|Catalyst::Manual::Tutorial::06_Authorization> |
d442cc9f |
37 | |
38 | =item 7 |
39 | |
3ab6187c |
40 | B<07_Debugging> |
d442cc9f |
41 | |
42 | =item 8 |
43 | |
3ab6187c |
44 | L<Testing|Catalyst::Manual::Tutorial::08_Testing> |
d442cc9f |
45 | |
46 | =item 9 |
47 | |
3ab6187c |
48 | L<Advanced CRUD|Catalyst::Manual::Tutorial::09_AdvancedCRUD> |
3533daff |
49 | |
50 | =item 10 |
51 | |
3ab6187c |
52 | L<Appendices|Catalyst::Manual::Tutorial::10_Appendices> |
d442cc9f |
53 | |
54 | =back |
55 | |
56 | |
57 | =head1 DESCRIPTION |
58 | |
bf4d990b |
59 | This chapter of the tutorial takes a brief look at the primary options |
d442cc9f |
60 | available for troubleshooting Catalyst applications. |
61 | |
62 | Note that when it comes to debugging and troubleshooting, there are two |
63 | camps: |
64 | |
65 | =over 4 |
66 | |
67 | =item * |
68 | |
69 | Fans of C<log> and C<print> statements embedded in the code. |
70 | |
71 | =item * |
72 | |
73 | Fans of interactive debuggers. |
74 | |
75 | =back |
76 | |
77 | Catalyst is able to easily accommodate both styles of debugging. |
78 | |
1390ef0e |
79 | |
d442cc9f |
80 | =head1 LOG STATEMENTS |
81 | |
bf4d990b |
82 | Folks in the former group can use Catalyst's C<$c-E<gt>log> facility. |
83 | (See L<Catalyst::Log> for more detail.) For example, if you add the |
84 | following code to a controller action method: |
d442cc9f |
85 | |
86 | $c->log->info("Starting the foreach loop here"); |
3dba69ab |
87 | |
cae937d8 |
88 | $c->log->debug("Value of \$id is: ".$id); |
d442cc9f |
89 | |
90 | Then the Catalyst development server will display your message along |
1390ef0e |
91 | with the other debug output. To accomplish the same thing in a TT |
92 | template view use: |
d442cc9f |
93 | |
8a7c5151 |
94 | [% c.log.debug("This is a test log message") %] |
d442cc9f |
95 | |
bf4d990b |
96 | As with many other logging facilities, a method is defined for each of |
97 | the following "logging levels" (in increasing order of |
ebde193e |
98 | severity/importance): |
99 | |
100 | $c->log->debug |
101 | $c->log->info |
102 | $c->log->warn |
103 | $c->log->error |
104 | $c->log->fatal |
105 | |
bf4d990b |
106 | You can also use L<Data::Dumper> in both Catalyst code |
107 | (C<use Data::Dumper; $c-E<gt>log-E<gt>debug("\$var is: ".Dumper($var));)>) |
d442cc9f |
108 | and TT templates (C<[% Dumper.dump(book) %]>. |
109 | |
0d360ef7 |
110 | B<NOTE:> Whether you are a logging fanatic or not, we strongly recommend |
111 | that you take advantage of L<Log::Log4perl> or L<Log::Dispatch>. It's |
112 | easy to use L<Catalyst::Log> with either of these and they will provide |
113 | a huge amount of extra functionality that you will want in virtually |
114 | every production application you run or support. |
115 | |
1390ef0e |
116 | |
d442cc9f |
117 | =head1 RUNNING CATALYST UNDER THE PERL DEBUGGER |
118 | |
119 | Members of the interactive-debugger fan club will also be at home with |
120 | Catalyst applications. One approach to this style of Perl debugging is |
121 | to embed breakpoints in your code. For example, open |
122 | C<lib/MyApp/Controller/Books.pm> in your editor and add the |
123 | C<DB::single=1> line as follows inside the C<list> method (I like to |
124 | "left-justify" my debug statements so I don't forget to remove them, but |
125 | you can obviously indent them if you prefer): |
126 | |
ddfbd850 |
127 | sub list :Local { |
3533daff |
128 | # Retrieve the usual Perl OO '$self' for this object. $c is the Catalyst |
d442cc9f |
129 | # 'Context' that's used to 'glue together' the various components |
130 | # that make up the application |
131 | my ($self, $c) = @_; |
132 | |
133 | $DB::single=1; |
aa7ff325 |
134 | |
d442cc9f |
135 | # Retrieve all of the book records as book model objects and store in the |
136 | # stash where they can be accessed by the TT template |
3b1fa91b |
137 | $c->stash->{books} = [$c->model('DB::Book')->all]; |
3dba69ab |
138 | |
d442cc9f |
139 | # Set the TT template to use. You will almost always want to do this |
140 | # in your action methods. |
141 | $c->stash->{template} = 'books/list.tt2'; |
142 | } |
143 | |
bf4d990b |
144 | This causes the Perl Debugger to enter "single step mode" when this |
145 | command is encountered (it has no effect when Perl is run without the |
146 | C<-d> flag). |
d442cc9f |
147 | |
d0496197 |
148 | B<NOTE:> The C<DB> here is the Perl Debugger, not the DB model. |
149 | |
3b1fa91b |
150 | If you haven't done it already, enable SQL logging as before: |
151 | |
152 | $ export DBIC_TRACE=1 |
153 | |
bf4d990b |
154 | To now run the Catalyst development server under the Perl debugger, |
155 | simply prepend C<perl -d> to the front of C<script/myapp_server.pl>: |
d442cc9f |
156 | |
157 | $ perl -d script/myapp_server.pl |
158 | |
159 | This will start the interactive debugger and produce output similar to: |
160 | |
161 | $ perl -d script/myapp_server.pl |
162 | |
028b4e1a |
163 | Loading DB routines from perl5db.pl version 1.3 |
d442cc9f |
164 | Editor support available. |
165 | |
166 | Enter h or `h h' for help, or `man perldebug' for more help. |
167 | |
028b4e1a |
168 | main::(script/myapp_server.pl:16): my $debug = 0; |
d442cc9f |
169 | |
170 | DB<1> |
171 | |
172 | Press the C<c> key and hit C<Enter> to continue executing the Catalyst |
173 | development server under the debugger. Although execution speed will be |
174 | slightly slower than normal, you should soon see the usual Catalyst |
175 | startup debug information. |
176 | |
177 | Now point your browser to L<http://localhost:3000/books/list> and log |
178 | in. Once the breakpoint is encountered in the |
179 | C<MyApp::Controller::list> method, the console session running the |
180 | development server will drop to the Perl debugger prompt: |
181 | |
0d360ef7 |
182 | MyApp::Controller::Books::list(/root/MyApp/script/../lib/MyApp/Controller/Books.pm:48): |
3b1fa91b |
183 | 48: $c->stash->{books} = [$c->model('DB::Book')->all]; |
d442cc9f |
184 | |
185 | DB<1> |
186 | |
187 | You now have the full Perl debugger at your disposal. First use the |
188 | C<next> feature by typing C<n> to execute the C<all> method on the Book |
189 | model (C<n> jumps over method/subroutine calls; you can also use C<s> to |
190 | C<single-step> into methods/subroutines): |
191 | |
192 | DB<1> n |
3b1fa91b |
193 | SELECT me.id, me.title, me.rating, me.created, me.updated FROM book me: |
0d360ef7 |
194 | MyApp::Controller::Books::list(/root/MyApp/script/../lib/MyApp/Controller/Books.pm:53): |
028b4e1a |
195 | 53: $c->stash->{template} = 'books/list.tt2'; |
d442cc9f |
196 | |
197 | DB<1> |
198 | |
199 | This takes you to the next line of code where the template name is set. |
bf4d990b |
200 | Notice that because we enabled C<DBIC_TRACE=1> earlier, SQL debug output |
201 | also shows up in the development server debug information. |
d442cc9f |
202 | |
203 | Next, list the methods available on our C<Book> model: |
204 | |
3b1fa91b |
205 | DB<1> m $c->model('DB::Book') |
d442cc9f |
206 | () |
207 | (0+ |
208 | (bool |
3b1fa91b |
209 | __result_class_accessor |
028b4e1a |
210 | __source_handle_accessor |
211 | _add_alias |
3b1fa91b |
212 | __bool |
028b4e1a |
213 | _build_unique_query |
214 | _calculate_score |
215 | _collapse_cond |
d442cc9f |
216 | <lines removed for brevity> |
217 | |
218 | DB<2> |
219 | |
220 | We can also play with the model directly: |
221 | |
3b1fa91b |
222 | DB<2> x ($c->model('DB::Book')->all)[1]->title |
223 | SELECT me.id, me.title, me.rating, me.created, me.updated FROM book me: |
d442cc9f |
224 | 0 'TCP/IP Illustrated, Volume 1' |
225 | |
226 | This uses the Perl debugger C<x> command to display the title of a book. |
227 | |
228 | Next we inspect the C<books> element of the Catalyst C<stash> (the C<4> |
229 | argument to the C<x> command limits the depth of the dump to 4 levels): |
230 | |
231 | DB<3> x 4 $c->stash->{books} |
232 | 0 ARRAY(0xa8f3b7c) |
d0496197 |
233 | 0 MyApp::Model::DB::Book=HASH(0xb8e702c) |
d442cc9f |
234 | '_column_data' => HASH(0xb8e5e2c) |
3b1fa91b |
235 | 'created' => '2009-05-08 10:19:46' |
d442cc9f |
236 | 'id' => 1 |
237 | 'rating' => 5 |
238 | 'title' => 'CCSP SNRS Exam Certification Guide' |
3b1fa91b |
239 | 'updated' => '2009-05-08 10:19:46' |
d442cc9f |
240 | '_in_storage' => 1 |
241 | <lines removed for brevity> |
242 | |
243 | Then enter the C<c> command to continue processing until the next |
244 | breakpoint is hit (or the application exits): |
245 | |
246 | DB<4> c |
247 | SELECT author.id, author.first_name, author.last_name FROM ... |
248 | |
249 | Finally, press C<Ctrl+C> to break out of the development server. |
250 | Because we are running inside the Perl debugger, you will drop to the |
bf4d990b |
251 | debugger prompt. |
3b1fa91b |
252 | |
253 | ^CCatalyst::Engine::HTTP::run(/usr/local/share/perl/5.10.0/Catalyst/Engine/HTTP.pm:260): |
254 | 260: while ( accept( Remote, $daemon ) ) { |
255 | |
256 | DB<4> |
257 | |
bf4d990b |
258 | Finally, press C<q> to exit the debugger and return to your OS shell |
259 | prompt: |
d442cc9f |
260 | |
261 | DB<4> q |
262 | $ |
263 | |
264 | For more information on using the Perl debugger, please see C<perldebug> |
3b1fa91b |
265 | and C<perldebtut>. For those daring souls out there, you can dive down |
266 | even deeper into the magical depths of this fine debugger by checking |
267 | out C<perldebguts>. |
d442cc9f |
268 | |
3b1fa91b |
269 | You can also type C<h> or C<h h> at the debugger prompt to view the |
270 | built-in help screens. |
271 | |
bf4d990b |
272 | For an excellent book covering all aspects of the Perl debugger, we |
273 | highly recommend reading 'Pro Perl Debugging' by Richard Foley. |
3b1fa91b |
274 | |
bf4d990b |
275 | Oh yeah, before you forget, be sure to remove the C<DB::single=1> line |
276 | you added above in C<lib/MyApp/Controller/Books.pm>. |
d442cc9f |
277 | |
278 | =head1 DEBUGGING MODULES FROM CPAN |
279 | |
bf4d990b |
280 | Although the techniques discussed above work well for code you are |
281 | writing, what if you want to use print/log/warn messages or set |
282 | breakpoints in code that you have installed from CPAN (or in module that |
283 | ship with Perl)? One helpful approach is to place a copy of the module |
284 | inside the C<lib> directory of your Catalyst project. When Catalyst |
285 | loads, it will load from inside your C<lib> directory first, only |
286 | turning to the global modules if a local copy cannot be found. You can |
287 | then make modifications such as adding a C<$DB::single=1> to the local |
288 | copy of the module without risking the copy in the original location. |
289 | This can also be a great way to "locally override" bugs in modules while |
d442cc9f |
290 | you wait for a fix on CPAN. |
291 | |
bf4d990b |
292 | Matt Trout has suggested the following shortcut to create a local copy |
293 | of an installed module: |
d442cc9f |
294 | |
295 | mkdir -p lib/Module; cp `perldoc -l Module::Name` lib/Module/ |
296 | |
bf4d990b |
297 | Note: If you are following along in Debian 6 or Ubuntu, you will need to |
298 | install the C<perl-doc> package to use the C<perldoc> command. Use |
299 | C<sudo aptitude install perl-doc> to do that. |
3533daff |
300 | |
bf4d990b |
301 | For example, you could make a copy of |
302 | L<Catalyst::Plugin::Authentication> with the following command: |
d442cc9f |
303 | |
304 | mkdir -p lib/Catalyst/Plugin; cp \ |
305 | `perldoc -l Catalyst::Plugin::Authentication` lib/Catalyst/Plugin |
306 | |
3533daff |
307 | You can then use the local copy inside your project to place logging |
308 | messages and/or breakpoints for further study of that module. |
309 | |
bf4d990b |
310 | B<Note:> Matt has also suggested the following tips for Perl debugging: |
d442cc9f |
311 | |
312 | =over 4 |
313 | |
314 | =item * |
315 | |
316 | Check the version of an installed module: |
317 | |
d672dfd7 |
318 | perl -M<mod_name> -e 'print "$<mod_name>::VERSION\n"' |
d442cc9f |
319 | |
320 | For example: |
321 | |
322 | $ perl -MCatalyst::Plugin::Authentication -e \ |
323 | 'print $Catalyst::Plugin::Authentication::VERSION;' |
324 | 0.07 |
325 | |
3b1fa91b |
326 | and if you are using bash aliases: |
327 | |
328 | alias pmver="perl -le '\$m = shift; eval qq(require \$m) \ |
329 | or die qq(module \"\$m\" is not installed\\n); \ |
330 | print \$m->VERSION'" |
331 | |
d442cc9f |
332 | =item * |
333 | |
334 | Check if a modules contains a given method: |
335 | |
336 | perl -MModule::Name -e 'print Module::Name->can("method");' |
337 | |
338 | For example: |
339 | |
340 | $ perl -MCatalyst::Plugin::Authentication -e \ |
3533daff |
341 | 'print Catalyst::Plugin::Authentication->can("user");' |
d442cc9f |
342 | CODE(0x9c8db2c) |
343 | |
344 | If the method exists, the Perl C<can> method returns a coderef. |
345 | Otherwise, it returns undef and nothing will be printed. |
346 | |
347 | =back |
348 | |
349 | |
1390ef0e |
350 | =head1 TT DEBUGGING |
351 | |
bf4d990b |
352 | If you run into issues during the rendering of your template, it might |
353 | be helpful to enable TT C<DEBUG> options. You can do this in a Catalyst |
354 | environment by adding a C<DEBUG> line to the C<__PACKAGE__->config> |
1edbdee6 |
355 | declaration in C<lib/MyApp/View/HTML.pm>: |
1390ef0e |
356 | |
357 | __PACKAGE__->config({ |
358 | TEMPLATE_EXTENSION => '.tt2', |
359 | DEBUG => 'undef', |
360 | }); |
361 | |
bf4d990b |
362 | There are a variety of options you can use, such as 'undef', 'all', |
363 | 'service', 'context', 'parser' and 'provider'. See |
364 | L<Template::Constants> for more information (remove the C<DEBUG_> |
365 | portion of the name shown in the TT docs and convert to lower case for |
366 | use inside Catalyst). |
1390ef0e |
367 | |
bf4d990b |
368 | B<NOTE:> B<Please be sure to disable TT debug options before continuing |
369 | with the tutorial> (especially the 'undef' option -- leaving this |
370 | enabled will conflict with several of the conventions used by this |
1390ef0e |
371 | tutorial to leave some variables undefined on purpose). |
372 | |
3b1fa91b |
373 | Happy debugging. |
1390ef0e |
374 | |
d442cc9f |
375 | =head1 AUTHOR |
376 | |
377 | Kennedy Clark, C<hkclark@gmail.com> |
378 | |
53243324 |
379 | Feel free to contact the author for any errors or suggestions, but the |
380 | best way to report issues is via the CPAN RT Bug system at |
381 | <https://rt.cpan.org/Public/Dist/Display.html?Name=Catalyst-Manual>. |
382 | |
383 | The most recent version of the Catalyst Tutorial can be found at |
59884771 |
384 | L<http://dev.catalyst.perl.org/repos/Catalyst/Catalyst-Manual/5.80/trunk/lib/Catalyst/Manual/Tutorial/>. |
d442cc9f |
385 | |
ec3ef4ad |
386 | Copyright 2006-2010, Kennedy Clark, under the |
387 | Creative Commons Attribution Share-Alike License Version 3.0 |
1390ef0e |
388 | (L<http://creativecommons.org/licenses/by-sa/3.0/us/>). |