sdsaran has asked for the wisdom of the Perl Monks concerning the following question:

Please give me the best way to debug in MVC architecture (CGI::Application and HTML::Template) based develeopment. Currently I am debugging with "print STDERR". It took too much time to find out the error position.

Replies are listed 'Best First'.
Re: Best way to debug in MVC
by redhotpenguin (Deacon) on Jun 02, 2005 at 05:25 UTC
    I use Test::More and Test::WWW::Mechanize to write unit tests for the models and controllers, and use Log::Log4perl to print debugging information in the modules to the error log. I use the perl debugger to step through each of the tests while I write them akin to the technique described in Interactive scripting with debugger.

    It takes some patience and practice, but I can find errors quickly because of the Log::Log4perl output which contains module line numbers, and once the test is passing I rarely have to come back to the same problem.

Re: Best way to debug in MVC
by eXile (Priest) on Jun 02, 2005 at 05:28 UTC
    I'm afraid the 'best way' to debug anything depends on what you are debugging, what the exact problem is you are debugging, and your debugging preferences. So I'm afraid I can't give you the 'best way'.

    I sometimes use a debug switch in my code at critical points like:

    my $DEBUG=1; ... my $val = get_something("vars"); warn $val if ($DEBUG && ! $val); ... my $val2 = do_something_else($val); warn $val2 if ($DEBUG && $val2 =~ /^\w{2}\d{2}$/);
    This way you can turn off/on debugging by (un)setting the master-switch $DEBUG, and even use different $DEBUG values for different types of debugging, the sky is the limit.

    If you want to see the call tree of your program you could use Devel::Dprof.

    If you want to see the content of complex structures, you could use Data::Dumper.

    Some people really like the perl debugger, you can do some cool tricks with it, see the perldebug docs.

      Apart from warn you have other options carp,cloak confess which gives full stack trace or minimal stack trace
      You can also use a similiar thing as mentioned before
      $DEBUG=1;
      ...
      my $val = get_something("vars"); warn $val if ($DEBUG && ! $val);
      But with a change istead of warn use mail which mails the errors to you alone instead of Showing it to entire world :=)
      Regards
      P.B.Sathish Kumar
Re: Best way to debug in MVC
by astroboy (Chaplain) on Jun 02, 2005 at 07:04 UTC
    I tend to use Log::Log4perl with CGI:Application because it's what I used before discovering CGI::App, but if you don't have a preference then CGI::Application::Plugin::LogDispatch may suit.
Re: Best way to debug in MVC
by perrin (Chancellor) on Jun 02, 2005 at 14:07 UTC
    It's no different from debugging anything else -- you take a quick stab with print statements and if that doesn't work you run the debugger. Are you finding it harder to debug under CGI::Application for some reason?