Cleaned up Tut files; no substantive changes
[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
5
6
7=head1 OVERVIEW
8
9This is B<Part 6 of 9> for the Catalyst tutorial.
10
64ccd8a8 11L<Tutorial Overview|Catalyst::Manual::Tutorial>
4d583dd8 12
13=over 4
14
15=item 1
16
17L<Introduction|Catalyst::Manual::Tutorial::Intro>
18
19=item 2
20
21L<Catalyst Basics|Catalyst::Manual::Tutorial::CatalystBasics>
22
23=item 3
24
64ccd8a8 25L<Basic CRUD|Catalyst::Manual::Tutorial_BasicCRUD>
4d583dd8 26
27=item 4
28
29L<Authentication|Catalyst::Manual::Tutorial::Authentication>
30
31=item 5
32
33L<Authorization|Catalyst::Manual::Tutorial::Authorization>
34
35=item 6
36
37B<Debugging>
38
39=item 7
40
41L<Testing|Catalyst::Manual::Tutorial::Testing>
42
43=item 8
44
45L<AdvancedCRUD|Catalyst::Manual::Tutorial::AdvancedCRUD>
46
47=item 9
48
49L<Appendicies|Catalyst::Manual::Tutorial::Appendicies>
50
51=back
52
53
54
55=head1 DESCRIPTION
56
64ccd8a8 57This part of the tutorial takes a brief look at the primary options
58available for troubleshooting Catalyst applications.
4d583dd8 59
60Note that when it comes to debugging and troubleshooting, there are two camps:
61
62=over 4
63
64=item *
65
66Fans of C<log> and C<print> statements embedded in the code.
67
68=item *
69
70Fans of interactive debuggers.
71
72=back
73
74Catalyst is able to easily accommodate both styles of debugging.
75
76
77
78=head1 LOG STATEMENTS
79
64ccd8a8 80Folks in the former group can use Catalyst's C<$c-E<gt>log> facility.
81For example, if you add the following code to a controller action
82method:
4d583dd8 83
84 $c->log->debug("This is a test log message");
85
64ccd8a8 86Then the Catalyst development server will display your message along
87with the other debug output. To accomplish the same thing in a TTSite
88view use:
4d583dd8 89
90 [% Catalyst.log.debug("This is a test log message") %]
91
64ccd8a8 92You can also use L<Data::Dumper|Data::Dumper> in both Catalyst code
93(C<$c-E<gt>log-E<gt>dumper($myvar)>) and TT templates (C<[%
94Dumper.dump(book) %]> as discussed in earlier parts of the tutorial.
4d583dd8 95
96
97
98=head1 RUNNING CATALYST UNDER THE PERL DEBUGGER
99
64ccd8a8 100Members of the interactive debuggers fan club will also be at home with
101Catalyst applications. One approach to this style of Perl debugging is
102to embed breakpoints in your code. For example, open
103C<lib/MyApp/Controller/Books.pm> in your editor and add the
104C<DB::single=1> line as follows inside the C<list> method (I like to
105"left-justify" my debug statements so I don't forget to remove them, but
106you can obviously indent them if you prefer):
4d583dd8 107
108 sub list : Local {
109 # Retrieve the usual perl OO '$self' for this object. $c is the Catalyst
110 # 'Context' that's used to 'glue together' the various components
111 # that make up the application
112 my ($self, $c) = @_;
113
114 $DB::single=1;
115
116 # Retrieve all of the book records as book model objects and store in the
117 # stash where they can be accessed by the TT template
118 $c->stash->{books} = [$c->model('MyAppDB::Book')->all];
119
120 # Set the TT template to use. You will almost always want to do this
121 # in your action methods.
122 $c->stash->{template} = 'books/list.tt2';
123 }
124
125This causes the Perl Debugger to enter "single step mode" when this command is
126encountered (it has no effect when Perl is run without the C<-d> flag).
127
128To now run the Catalyst development server under the Perl debugger, simply
129prepend C<perl -d> to the front of C<script/myapp_server.pl>:
130
131 $ perl -d script/myapp_server.pl
132
133This will start the interactive debugger and produce output similar to:
134
135 $ perl -d script/myapp_server.pl
136
137 Loading DB routines from perl5db.pl version 1.27
138 Editor support available.
139
140 Enter h or `h h' for help, or `man perldebug' for more help.
141
142 main::(script/myapp_server.pl:14): my $debug = 0;
143
144 DB<1>
145
64ccd8a8 146Press the C<c> key and hit C<Enter> to continue executing the Catalyst
147development server under the debugger. Although execution speed will be
148slightly slower than normal, you should soon see the usual Catalyst
149startup debug information.
4d583dd8 150
64ccd8a8 151Now point your browser to L<http://localhost:3000/books/list> and log
152in. Once the breakpoint is encountered in the
153C<MyApp::Controller::list> method, the console session running the
154development server will drop to the Perl debugger prompt:
4d583dd8 155
156 MyApp::Controller::Books::list(/home/me/MyApp/script/../lib/MyApp/Controller/Books.pm:40):
157 40: $c->stash->{books} = [$c->model('MyAppDB::Book')->all];
158
159 DB<1>
160
64ccd8a8 161You now have the full Perl debugger at your disposal. First use the
162C<next> feature by typing C<n> to execute the C<all> method on the Book
163model (C<n> jumps over method/subroutine calls; you can also use C<s> to
164C<single-step> into methods/subroutines):
4d583dd8 165
166 DB<1> n
167 SELECT me.id, me.authors, me.title, me.rating FROM books me:
168 MyApp::Controller::Books::list(/home/me/MyApp/script/../lib/MyApp/Controller/Books.pm:44):
169 44: $c->stash->{template} = 'books/list.tt2';
170
171 DB<1>
172
64ccd8a8 173This takes you to the next line of code where the template name is set.
174Notice that because we enabled C<DBIX_CLASS_STORAGE_DBI_DEBUG=1>
175earlier, SQL debug output also shows up in the development server debug
176output.
4d583dd8 177
178Next, list the methods available on our C<Book> model:
179
180 DB<1> m $c->model('MyAppDB::Book')
181 ()
182 (0+
183 (bool
184 MODIFY_CODE_ATTRIBUTES
185 _attr_cache
186 _collapse_result
187 _construct_object
188 _count
189 _result_class_accessor
190 _result_source_accessor
191 all
192 carp
193 <lines removed for brevity>
194
195 DB<2>
196
197We can also play with the model directly:
198
199 DB<2> x ($c->model('MyAppDB::Book')->all)[1]->title
200 SELECT me.id, me.title, me.rating FROM books me:
201 0 'TCP/IP Illustrated, Volume 1'
202
203This uses the Perl debugger C<x> command to display the title of a book.
204
64ccd8a8 205Next we inspect the C<books> element of the Catalyst C<stash> (the C<4>
206argument to the C<x> command limits the depth of the dump to 4 levels):
4d583dd8 207
208 DB<3> x 4 $c->stash->{books}
209 0 ARRAY(0xa8f3b7c)
210 0 MyApp::Model::MyAppDB::Book=HASH(0xb8e702c)
211 '_column_data' => HASH(0xb8e5e2c)
212 'id' => 1
213 'rating' => 5
214 'title' => 'CCSP SNRS Exam Certification Guide'
215 '_in_storage' => 1
216 <lines removed for brevity>
217
64ccd8a8 218Then enter the C<c> command to continue processing until the next
219breakpoint is hit (or the application exits):
4d583dd8 220
221 DB<4> c
222 SELECT author.id, author.first_name, author.last_name FROM ...
223
64ccd8a8 224Finally, press C<Ctrl+C> to break out of the development server.
225Because we are running inside the Perl debugger, you will drop to the
226debugger prompt. Press C<q> to exit the debugger and return to your OS
227shell prompt:
4d583dd8 228
229 DB<4> q
230 $
231
64ccd8a8 232For more information on using the Perl debugger, please see C<perldebug>
233and C<perldebtut>. You can also type C<h> or C<h h> at the debugger
234prompt to view the built-in help screens.
4d583dd8 235
236
237
238=head1 AUTHOR
239
240Kennedy Clark, C<hkclark@gmail.com>
241
242Please report any errors, issues or suggestions to the author.
243
64ccd8a8 244Copyright 2006, Kennedy Clark, under Creative Commons License (L<http://creativecommons.org/licenses/by-nc-sa/2.5/>).
4d583dd8 245
246Version: .94
247