Oktober 2011 Archive

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:

Über dieses Archiv

Diese Seite enthält alle Einträge von Perl HowTo von neu nach alt.

September 2011 ist das vorherige Archiv.

November 2011 ist das nächste Archiv.

Aktuelle Einträge finden Sie auf der Startseite, alle Einträge in den Archiven.

Blog Roll

Powered by

Powered by Movable Type 5.2.10

Creative Commons-Lizenz

Creative Commons License
Dieses Weblog steht unter einer Creative Commons-Lizenz.