From: Arthur Axel 'fREW' Schmidt Date: Sat, 23 Oct 2010 03:06:02 +0000 (-0500) Subject: Create Ddie for die'ing output X-Git-Tag: v2.020~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2566c738d28b5c6fd48615bd8a7a297d3e92cffd;p=p5sagit%2FData-Dumper-Concise.git Create Ddie for die'ing output --- diff --git a/Changes b/Changes index 0784104..9e77f22 100644 --- a/Changes +++ b/Changes @@ -1,4 +1,5 @@ - create DwarnF for formatting Dumper'd output + - Create Ddie for die'ing output 2.012 Aug 31 2010 - Make DwarnN test still work if Devel::ArgNames isn't installed diff --git a/lib/Data/Dumper/Concise/Sugar.pm b/lib/Data/Dumper/Concise/Sugar.pm index 5aef1a1..f82754a 100644 --- a/lib/Data/Dumper/Concise/Sugar.pm +++ b/lib/Data/Dumper/Concise/Sugar.pm @@ -7,7 +7,10 @@ use Data::Dumper::Concise (); BEGIN { @ISA = qw(Exporter) } -@EXPORT = qw($Dwarn $DwarnN Dwarn DwarnS DwarnL DwarnN DwarnF); +@EXPORT = qw( + $Dwarn $DwarnN Dwarn DwarnS DwarnL DwarnN + $Ddie $DdieN Ddie DdieS DdieL DdieN DdieD +); sub Dwarn { return DwarnL(@_) if wantarray; DwarnS($_[0]) } @@ -26,6 +29,21 @@ sub DwarnN ($) { sub DwarnF (&@) { my $c = shift; warn &Data::Dumper::Concise::DumperF($c, @_); @_ } +sub Ddie { DdieL(@_) if wantarray; DdieS($_[0]) } + +our $Ddie = \&Ddie; +our $DdieN = \&DdieN; + +sub DdieL { die Data::Dumper::Concise::Dumper @_ } + +sub DdieS ($) { die Data::Dumper::Concise::Dumper $_[0] } + +sub DdieN ($) { + require Devel::ArgNames; + my $x = Devel::ArgNames::arg_names(); + die(($x?$x:'(anon)') . ' => ' . Data::Dumper::Concise::Dumper $_[0]); +} + =head1 NAME Data::Dumper::Concise::Sugar - return Dwarn @return_value @@ -112,6 +130,12 @@ is equivalent to: warn DumperF { "awesome: $_[0] not awesome: $_[1]" } $awesome, $cheesy; return @return; +If you want to immediately die after outputting the data structure, every +Dwarn subroutine has a paired Ddie version, so just replace the warn with die. +For example: + + DdieL 'foo', { bar => 'baz' }; + =head1 DESCRIPTION use Data::Dumper::Concise::Sugar; diff --git a/lib/Devel/Dwarn.pm b/lib/Devel/Dwarn.pm index 01da500..6896371 100644 --- a/lib/Devel/Dwarn.pm +++ b/lib/Devel/Dwarn.pm @@ -82,6 +82,12 @@ is equivalent to: warn Dumper($return); return $return; +If you want to immediately die after outputting the data structure, every +Dwarn subroutine has a paired Ddie version, so just replace the warn with die. +For example: + + DdieL 'foo', { bar => 'baz' }; + =head1 TIPS AND TRICKS =head2 global usage diff --git a/t/sugar.t b/t/sugar.t index 817f4ad..20eb013 100644 --- a/t/sugar.t +++ b/t/sugar.t @@ -66,3 +66,10 @@ DWARNN: { } } +DDIE: { + eval { + DdieS [ 'k', 'bar' ]; + }; + is $@, qq{[\n "k",\n "bar"\n]\n}, 'DwarnD dies output correctly'; +} +