Carp::Always - Warns and dies noisily with stack backtraces von Adriano Ferreira erleichtert das Debuggen von Perl-Programmen.
Statt mühselig den Debugger an zu werfen, wird Carp::Always einfach ohne Änderung des Programms auf der Kommandozeile aufgerufen.
$ perl -MCarp::Always programm.pl
Beispiel:
Das folgende Beispielprogramm versucht eine nicht existierende Webseite auf zu rufen.
#!/usr/bin/perl use strict; use warnings; use WWW::Mechanize; my $mech = WWW::Mechanize->new(); my $url = 'http://example.tld'; $mech->get( $url );
Das Programm erzeugt folgende Ausgabe:
Error GETing http://example.tld: Can't connect to example.tld:80 (Bad hostname 'example.tld') at mech.pl line 11
Carp::Always wird auf der Kommandozeile aufgerufen
$ /opt/perl-5.12.3/bin/perl -MCarp::Always mech.pl
und erzeugt folgenden Stack Backtrace (den ich hier ein wenig umgebrochen habe):
Error GETing http://example.tld: Can't connect to example.tld:80 (Bad hostname) at /opt/perl-5.12.3/lib/site_perl/5.12.3/WWW/Mechanize.pm line 2747 WWW::Mechanize::_die('Error ', 'GET', 'ing ', 'URI::http=SCALAR(0xa1f2db0)', ': ', 'Can\'t connect to example.tld:80 (Bad hostname)') called at /opt/perl-5.12.3/lib/site_perl/5.12.3/WWW/Mechanize.pm line 2734 WWW::Mechanize::die('WWW::Mechanize=HASH(0xa11cf98)', 'Error ', 'GET', 'ing ', 'URI::http=SCALAR(0xa1f2db0)', ': ', 'Can\'t connect to example.tld:80 (Bad hostname)') called at /opt/perl-5.12.3/lib/site_perl/5.12.3/WWW/Mechanize.pm line 2381 WWW::Mechanize::_update_page('WWW::Mechanize=HASH(0xa11cf98)', 'HTTP::Request=HASH(0xa211690)', 'HTTP::Response=HASH(0xa15f480)') called at /opt/perl-5.12.3/lib/site_perl/5.12.3/WWW/Mechanize.pm line 2213 WWW::Mechanize::request('WWW::Mechanize=HASH(0xa11cf98)', 'HTTP::Request=HASH(0xa211690)') called at /opt/perl-5.12.3/lib/site_perl/5.12.3/LWP/UserAgent.pm line 411 LWP::UserAgent::get('WWW::Mechanize=HASH(0xa11cf98)', 'http://example.tld') called at /opt/perl-5.12.3/lib/site_perl/5.12.3/WWW/Mechanize.pm line 407 WWW::Mechanize::get('WWW::Mechanize=HASH(0xa11cf98)', 'http://example.tld') called at /opt/perl-5.12.3/lib/site_perl/5.12.3/WWW/Mechanize.pm line 2747 WWW::Mechanize::_die('Error ', 'GET', 'ing ', 'URI::http=SCALAR(0xa1f2db0)', ': ', 'Can\'t connect to example.tld:80 (Bad hostname)') called at /opt/perl-5.12.3/lib/site_perl/5.12.3/WWW/Mechanize.pm line 2734 WWW::Mechanize::die('WWW::Mechanize=HASH(0xa11cf98)', 'Error ', 'GET', 'ing ', 'URI::http=SCALAR(0xa1f2db0)', ': ', 'Can\'t connect to example.tld:80 (Bad hostname)') called at /opt/perl-5.12.3/lib/site_perl/5.12.3/WWW/Mechanize.pm line 2381 WWW::Mechanize::_update_page('WWW::Mechanize=HASH(0xa11cf98)', 'HTTP::Request=HASH(0xa211690)', 'HTTP::Response=HASH(0xa15f480)') called at /opt/perl-5.12.3/lib/site_perl/5.12.3/WWW/Mechanize.pm line 2213 WWW::Mechanize::request('WWW::Mechanize=HASH(0xa11cf98)', 'HTTP::Request=HASH(0xa211690)') called at /opt/perl-5.12.3/lib/site_perl/5.12.3/LWP/UserAgent.pm line 411 LWP::UserAgent::get('WWW::Mechanize=HASH(0xa11cf98)', 'http://example.tld') called at /opt/perl-5.12.3/lib/site_perl/5.12.3/WWW/Mechanize.pm line 407 WWW::Mechanize::get('WWW::Mechanize=HASH(0xa11cf98)', 'http://example.tld') called at mech.pl line 11
Wem Schwarz-Weiß zu langweilig ist, kann die Ausgabe mit Carp::Always::Color aufhübschen.
$ perl -MCarp::Always::Color programm.pl
Hinweis:
Da Carp::Always eigene Signalhandler für die() und warn() installiert, funktioniert dieses Modul nicht mit Programmen oder Modulen, die bereits Signalhandler für die() und/oder warn() verwenden.
Siehe auch:
- Carp::Always - Warns and dies noisily with stack backtraces
- CPAN Ratings Carp-Always reviews
- Perl Module Monday: Carp::Always
- Carp::Always::Color - Carp::Always, but with color
- Carp::Source::Always - Warns and dies with stack backtraces and source code context
- Devel::SimpleTrace - See where you code warns and dies using stack traces
- Carp
- diagnostics
- Perl 101: Debugging
Jetzt kommentieren