3 Catalyst::Manual::Tutorial::Debugging - Catalyst Tutorial - Part 6: Debugging
9 This is B<Part 6 of 9> for the Catalyst tutorial.
11 L<Tutorial Overview|Catalyst::Manual::Tutorial>
17 L<Introduction|Catalyst::Manual::Tutorial::Intro>
21 L<Catalyst Basics|Catalyst::Manual::Tutorial::CatalystBasics>
25 L<Basic CRUD|Catalyst::Manual::Tutorial_BasicCRUD>
29 L<Authentication|Catalyst::Manual::Tutorial::Authentication>
33 L<Authorization|Catalyst::Manual::Tutorial::Authorization>
41 L<Testing|Catalyst::Manual::Tutorial::Testing>
45 L<AdvancedCRUD|Catalyst::Manual::Tutorial::AdvancedCRUD>
49 L<Appendicies|Catalyst::Manual::Tutorial::Appendicies>
57 This part of the tutorial takes a brief look at the primary options
58 available for troubleshooting Catalyst applications.
60 Note that when it comes to debugging and troubleshooting, there are two camps:
66 Fans of C<log> and C<print> statements embedded in the code.
70 Fans of interactive debuggers.
74 Catalyst is able to easily accommodate both styles of debugging.
80 Folks in the former group can use Catalyst's C<$c-E<gt>log> facility.
81 For example, if you add the following code to a controller action
84 $c->log->debug("This is a test log message");
86 Then the Catalyst development server will display your message along
87 with the other debug output. To accomplish the same thing in a TTSite
90 [% Catalyst.log.debug("This is a test log message") %]
92 You 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<[%
94 Dumper.dump(book) %]> as discussed in earlier parts of the tutorial.
98 =head1 RUNNING CATALYST UNDER THE PERL DEBUGGER
100 Members of the interactive debuggers fan club will also be at home with
101 Catalyst applications. One approach to this style of Perl debugging is
102 to embed breakpoints in your code. For example, open
103 C<lib/MyApp/Controller/Books.pm> in your editor and add the
104 C<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
106 you can obviously indent them if you prefer):
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
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];
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';
125 This causes the Perl Debugger to enter "single step mode" when this command is
126 encountered (it has no effect when Perl is run without the C<-d> flag).
128 To now run the Catalyst development server under the Perl debugger, simply
129 prepend C<perl -d> to the front of C<script/myapp_server.pl>:
131 $ perl -d script/myapp_server.pl
133 This will start the interactive debugger and produce output similar to:
135 $ perl -d script/myapp_server.pl
137 Loading DB routines from perl5db.pl version 1.27
138 Editor support available.
140 Enter h or `h h' for help, or `man perldebug' for more help.
142 main::(script/myapp_server.pl:14): my $debug = 0;
146 Press the C<c> key and hit C<Enter> to continue executing the Catalyst
147 development server under the debugger. Although execution speed will be
148 slightly slower than normal, you should soon see the usual Catalyst
149 startup debug information.
151 Now point your browser to L<http://localhost:3000/books/list> and log
152 in. Once the breakpoint is encountered in the
153 C<MyApp::Controller::list> method, the console session running the
154 development server will drop to the Perl debugger prompt:
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];
161 You now have the full Perl debugger at your disposal. First use the
162 C<next> feature by typing C<n> to execute the C<all> method on the Book
163 model (C<n> jumps over method/subroutine calls; you can also use C<s> to
164 C<single-step> into methods/subroutines):
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';
173 This takes you to the next line of code where the template name is set.
174 Notice that because we enabled C<DBIX_CLASS_STORAGE_DBI_DEBUG=1>
175 earlier, SQL debug output also shows up in the development server debug
178 Next, list the methods available on our C<Book> model:
180 DB<1> m $c->model('MyAppDB::Book')
184 MODIFY_CODE_ATTRIBUTES
189 _result_class_accessor
190 _result_source_accessor
193 <lines removed for brevity>
197 We can also play with the model directly:
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'
203 This uses the Perl debugger C<x> command to display the title of a book.
205 Next we inspect the C<books> element of the Catalyst C<stash> (the C<4>
206 argument to the C<x> command limits the depth of the dump to 4 levels):
208 DB<3> x 4 $c->stash->{books}
210 0 MyApp::Model::MyAppDB::Book=HASH(0xb8e702c)
211 '_column_data' => HASH(0xb8e5e2c)
214 'title' => 'CCSP SNRS Exam Certification Guide'
216 <lines removed for brevity>
218 Then enter the C<c> command to continue processing until the next
219 breakpoint is hit (or the application exits):
222 SELECT author.id, author.first_name, author.last_name FROM ...
224 Finally, press C<Ctrl+C> to break out of the development server.
225 Because we are running inside the Perl debugger, you will drop to the
226 debugger prompt. Press C<q> to exit the debugger and return to your OS
232 For more information on using the Perl debugger, please see C<perldebug>
233 and C<perldebtut>. You can also type C<h> or C<h h> at the debugger
234 prompt to view the built-in help screens.
240 Kennedy Clark, C<hkclark@gmail.com>
242 Please report any errors, issues or suggestions to the author.
244 Copyright 2006, Kennedy Clark, under Creative Commons License (L<http://creativecommons.org/licenses/by-nc-sa/2.5/>).