Add link to most recent version in subversion so that people can easily locate the...
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Manual / Tutorial / Debugging.pod
CommitLineData
4d583dd8 1=head1 NAME
2
64ccd8a8 3Catalyst::Manual::Tutorial::Debugging - Catalyst Tutorial - Part 6: Debugging
4d583dd8 4
4d583dd8 5=head1 OVERVIEW
6
7This is B<Part 6 of 9> for the Catalyst tutorial.
8
64ccd8a8 9L<Tutorial Overview|Catalyst::Manual::Tutorial>
4d583dd8 10
11=over 4
12
13=item 1
14
15L<Introduction|Catalyst::Manual::Tutorial::Intro>
16
17=item 2
18
19L<Catalyst Basics|Catalyst::Manual::Tutorial::CatalystBasics>
20
21=item 3
22
64ccd8a8 23L<Basic CRUD|Catalyst::Manual::Tutorial_BasicCRUD>
4d583dd8 24
25=item 4
26
27L<Authentication|Catalyst::Manual::Tutorial::Authentication>
28
29=item 5
30
31L<Authorization|Catalyst::Manual::Tutorial::Authorization>
32
33=item 6
34
35B<Debugging>
36
37=item 7
38
39L<Testing|Catalyst::Manual::Tutorial::Testing>
40
41=item 8
42
43L<AdvancedCRUD|Catalyst::Manual::Tutorial::AdvancedCRUD>
44
45=item 9
46
c19d127e 47L<Appendices|Catalyst::Manual::Tutorial::Appendices>
4d583dd8 48
49=back
50
51
4d583dd8 52=head1 DESCRIPTION
53
64ccd8a8 54This part of the tutorial takes a brief look at the primary options
55available for troubleshooting Catalyst applications.
4d583dd8 56
c19d127e 57Note that when it comes to debugging and troubleshooting, there are two
58camps:
4d583dd8 59
60=over 4
61
62=item *
63
64Fans of C<log> and C<print> statements embedded in the code.
65
66=item *
67
68Fans of interactive debuggers.
69
70=back
71
72Catalyst is able to easily accommodate both styles of debugging.
73
4d583dd8 74=head1 LOG STATEMENTS
75
64ccd8a8 76Folks in the former group can use Catalyst's C<$c-E<gt>log> facility.
c19d127e 77(See L<Catalyst::Log> for more detail.) For example, if you add the
78following code to a controller action method:
4d583dd8 79
c19d127e 80 $c->log->info("Starting the foreach loop here");
81
82 $c->log->debug("Value of $id is: ".$id);
4d583dd8 83
64ccd8a8 84Then the Catalyst development server will display your message along
c19d127e 85with the other debug output. To accomplish the same thing in a TTSite
64ccd8a8 86view use:
4d583dd8 87
88 [% Catalyst.log.debug("This is a test log message") %]
89
cc548726 90You can also use L<Data::Dumper|Data::Dumper> in both Catalyst code
91(C<use Data::Dumper; $c-E<gt>log-E<gt>debug("$var is: ".Dumper($var));)>)
92and TT templates (C<[% Dumper.dump(book) %]>.
4d583dd8 93
94=head1 RUNNING CATALYST UNDER THE PERL DEBUGGER
95
c19d127e 96Members of the interactive-debugger fan club will also be at home with
64ccd8a8 97Catalyst applications. One approach to this style of Perl debugging is
98to embed breakpoints in your code. For example, open
99C<lib/MyApp/Controller/Books.pm> in your editor and add the
100C<DB::single=1> line as follows inside the C<list> method (I like to
101"left-justify" my debug statements so I don't forget to remove them, but
102you can obviously indent them if you prefer):
4d583dd8 103
104 sub list : Local {
105 # Retrieve the usual perl OO '$self' for this object. $c is the Catalyst
106 # 'Context' that's used to 'glue together' the various components
107 # that make up the application
108 my ($self, $c) = @_;
109
110 $DB::single=1;
111
112 # Retrieve all of the book records as book model objects and store in the
113 # stash where they can be accessed by the TT template
114 $c->stash->{books} = [$c->model('MyAppDB::Book')->all];
115
116 # Set the TT template to use. You will almost always want to do this
117 # in your action methods.
118 $c->stash->{template} = 'books/list.tt2';
119 }
120
121This causes the Perl Debugger to enter "single step mode" when this command is
122encountered (it has no effect when Perl is run without the C<-d> flag).
123
124To now run the Catalyst development server under the Perl debugger, simply
125prepend C<perl -d> to the front of C<script/myapp_server.pl>:
126
127 $ perl -d script/myapp_server.pl
128
129This will start the interactive debugger and produce output similar to:
130
131 $ perl -d script/myapp_server.pl
132
133 Loading DB routines from perl5db.pl version 1.27
134 Editor support available.
135
136 Enter h or `h h' for help, or `man perldebug' for more help.
137
138 main::(script/myapp_server.pl:14): my $debug = 0;
139
140 DB<1>
141
64ccd8a8 142Press the C<c> key and hit C<Enter> to continue executing the Catalyst
143development server under the debugger. Although execution speed will be
144slightly slower than normal, you should soon see the usual Catalyst
145startup debug information.
4d583dd8 146
64ccd8a8 147Now point your browser to L<http://localhost:3000/books/list> and log
148in. Once the breakpoint is encountered in the
149C<MyApp::Controller::list> method, the console session running the
150development server will drop to the Perl debugger prompt:
4d583dd8 151
152 MyApp::Controller::Books::list(/home/me/MyApp/script/../lib/MyApp/Controller/Books.pm:40):
153 40: $c->stash->{books} = [$c->model('MyAppDB::Book')->all];
154
155 DB<1>
156
64ccd8a8 157You now have the full Perl debugger at your disposal. First use the
158C<next> feature by typing C<n> to execute the C<all> method on the Book
159model (C<n> jumps over method/subroutine calls; you can also use C<s> to
160C<single-step> into methods/subroutines):
4d583dd8 161
162 DB<1> n
163 SELECT me.id, me.authors, me.title, me.rating FROM books me:
164 MyApp::Controller::Books::list(/home/me/MyApp/script/../lib/MyApp/Controller/Books.pm:44):
165 44: $c->stash->{template} = 'books/list.tt2';
166
167 DB<1>
168
64ccd8a8 169This takes you to the next line of code where the template name is set.
170Notice that because we enabled C<DBIX_CLASS_STORAGE_DBI_DEBUG=1>
171earlier, SQL debug output also shows up in the development server debug
172output.
4d583dd8 173
174Next, list the methods available on our C<Book> model:
175
176 DB<1> m $c->model('MyAppDB::Book')
177 ()
178 (0+
179 (bool
180 MODIFY_CODE_ATTRIBUTES
181 _attr_cache
182 _collapse_result
183 _construct_object
184 _count
185 _result_class_accessor
186 _result_source_accessor
187 all
188 carp
189 <lines removed for brevity>
190
191 DB<2>
192
193We can also play with the model directly:
194
195 DB<2> x ($c->model('MyAppDB::Book')->all)[1]->title
196 SELECT me.id, me.title, me.rating FROM books me:
197 0 'TCP/IP Illustrated, Volume 1'
198
199This uses the Perl debugger C<x> command to display the title of a book.
200
64ccd8a8 201Next we inspect the C<books> element of the Catalyst C<stash> (the C<4>
202argument to the C<x> command limits the depth of the dump to 4 levels):
4d583dd8 203
204 DB<3> x 4 $c->stash->{books}
205 0 ARRAY(0xa8f3b7c)
206 0 MyApp::Model::MyAppDB::Book=HASH(0xb8e702c)
207 '_column_data' => HASH(0xb8e5e2c)
208 'id' => 1
209 'rating' => 5
210 'title' => 'CCSP SNRS Exam Certification Guide'
211 '_in_storage' => 1
212 <lines removed for brevity>
213
64ccd8a8 214Then enter the C<c> command to continue processing until the next
215breakpoint is hit (or the application exits):
4d583dd8 216
217 DB<4> c
218 SELECT author.id, author.first_name, author.last_name FROM ...
219
64ccd8a8 220Finally, press C<Ctrl+C> to break out of the development server.
221Because we are running inside the Perl debugger, you will drop to the
222debugger prompt. Press C<q> to exit the debugger and return to your OS
223shell prompt:
4d583dd8 224
225 DB<4> q
226 $
227
64ccd8a8 228For more information on using the Perl debugger, please see C<perldebug>
229and C<perldebtut>. You can also type C<h> or C<h h> at the debugger
230prompt to view the built-in help screens.
4d583dd8 231
cc548726 232
233=head1 DEBUGGING MODULES FROM CPAN
234
235Although the techniques discussed above work well for code you are
236writing, what if you want to use print/log/warn messages or set
237breakpoints in code that you have installed from CPAN (or in module
238that ship with Perl)? One helpful approach is to place a copy of
239the module inside the C<lib> directory of your Catalyst project.
240When Catalyst loads, it will load from inside your C<lib> directory
241first, only turning to the global modules if a local copy cannot be
242found. You can then make modifications such as adding a
243C<$DB::single=1> to the local copy of the module without risking
244the copy in the original location.
245
246Matt Trout has suggested the following shortcut to create a local
247copy of an installed module:
248
249 mkdir -p lib/Module; cp `perldoc -l Module::Name` lib/Module/
250
251For example, you could make a copy of
252L<Catalyst::Plugin::Authentication|Catalyst::Plugin::Authentication>
253with the following command:
254
255 mkdir -p lib/Catalyst/Plugin; cp \
256 `perldoc -l Catalyst::Plugin::Authentication` lib/Catalyst/Plugin
257
258B<Note:> Matt has also suggested the following tips for Perl
259debugging:
260
261=over 4
262
263=item *
264
265Check the version of an installed module:
266
267 perl -MModule::Name -e 'print $Module::Name::VERSION;'
268
269For example:
270
271 $ perl -MCatalyst::Plugin::Authentication -e \
272 'print $Catalyst::Plugin::Authentication::VERSION;'
273 0.07
274
275=item *
276
277Check if a modules contains a given method:
278
279 perl -MModule::Name -e 'print Module::Name->can("method");'
280
281For example:
282
283 $ perl -MCatalyst::Plugin::Authentication -e \
284 'print Catalyst::Plugin::Authentication->can("prepare");'
285 CODE(0x9c8db2c)
286
287If the method exists, the Perl C<can> method returns a coderef.
288Otherwise, it returns undef and nothing will be printed.
289
290=back
291
292
4d583dd8 293=head1 AUTHOR
294
295Kennedy Clark, C<hkclark@gmail.com>
296
eed93301 297Please report any errors, issues or suggestions to the author. The
298most recent version of the Catlayst Tutorial can be found at
299L<http://dev.catalyst.perl.org/repos/Catalyst/trunk/Catalyst-Runtime/lib/Catalyst/Manual/Tutorial/>.
4d583dd8 300
c19d127e 301Copyright 2006, Kennedy Clark, under Creative Commons License
302(L<http://creativecommons.org/licenses/by-nc-sa/2.5/>).