From: Arthur Axel 'fREW' Schmidt Date: Sat, 23 Oct 2010 04:02:31 +0000 (-0500) Subject: create DwarnF for formatting Dumper'd output X-Git-Tag: v2.020~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=92264889a0f278aa61cdee48aef484496fb6e5ce;p=p5sagit%2FData-Dumper-Concise.git create DwarnF for formatting Dumper'd output --- diff --git a/Changes b/Changes index 7b432c7..0784104 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,5 @@ + - create DwarnF for formatting Dumper'd output + 2.012 Aug 31 2010 - Make DwarnN test still work if Devel::ArgNames isn't installed diff --git a/lib/Data/Dumper/Concise.pm b/lib/Data/Dumper/Concise.pm index 0ec1961..5beb1a5 100644 --- a/lib/Data/Dumper/Concise.pm +++ b/lib/Data/Dumper/Concise.pm @@ -9,7 +9,7 @@ require Data::Dumper; BEGIN { @ISA = qw(Exporter) } -@EXPORT = qw(Dumper); +@EXPORT = qw(Dumper DumperF); sub Dumper { my $dd = Data::Dumper->new([]); @@ -17,6 +17,11 @@ sub Dumper { return $dd->Values([ @_ ])->Dump; } +sub DumperF (&@) { + my $code = shift; + return $code->(map Dumper($_), @_); +} + =head1 NAME Data::Dumper::Concise - Less indentation and newlines plus sub deparsing @@ -65,6 +70,17 @@ instead of the default Data::Dumper output: (note the tab indentation, oh joy ...) +Also try out C which takes a C as the first argument to +format the output. For example: + + use Data::Dumper::Concise; + + warn DumperF { "result: $_[0] result2: $_[1]" } $foo, $bar; + +Which is the same as: + + warn 'result: ' . Dumper($foo) . ' result2: ' . Dumper($bar); + =head1 DESCRIPTION This module always exports a single function, Dumper, which can be called diff --git a/lib/Data/Dumper/Concise/Sugar.pm b/lib/Data/Dumper/Concise/Sugar.pm index 4b33717..5aef1a1 100644 --- a/lib/Data/Dumper/Concise/Sugar.pm +++ b/lib/Data/Dumper/Concise/Sugar.pm @@ -7,7 +7,7 @@ use Data::Dumper::Concise (); BEGIN { @ISA = qw(Exporter) } -@EXPORT = qw($Dwarn $DwarnN Dwarn DwarnS DwarnL DwarnN); +@EXPORT = qw($Dwarn $DwarnN Dwarn DwarnS DwarnL DwarnN DwarnF); sub Dwarn { return DwarnL(@_) if wantarray; DwarnS($_[0]) } @@ -24,6 +24,8 @@ sub DwarnN ($) { warn(($x?$x:'(anon)') . ' => ' . Data::Dumper::Concise::Dumper $_[0]); $_[0] } +sub DwarnF (&@) { my $c = shift; warn &Data::Dumper::Concise::DumperF($c, @_); @_ } + =head1 NAME Data::Dumper::Concise::Sugar - return Dwarn @return_value @@ -100,6 +102,16 @@ is equivalent to: warn Dumper($return); return $return; +If you want to format the output of your data structures, try DwarnF + + my ($a, $c) = DwarnF { "awesome: $_[0] not awesome: $_[1]" } $awesome, $cheesy; + +is equivalent to: + + my @return = ($awesome, $cheesy); + warn DumperF { "awesome: $_[0] not awesome: $_[1]" } $awesome, $cheesy; + return @return; + =head1 DESCRIPTION use Data::Dumper::Concise::Sugar; @@ -133,6 +145,10 @@ L, so see its docs for ways to make it do something else. B: this requires L to be installed. +=head2 DwarnF + + sub DwarnF (&@) { my $c = shift; warn &Data::Dumper::Concise::DumperF($c, @_); @_ } + =head1 TIPS AND TRICKS =head2 global usage diff --git a/t/concise.t b/t/concise.t index df5e48f..20e3512 100644 --- a/t/concise.t +++ b/t/concise.t @@ -33,3 +33,7 @@ foreach my $to_dump ( is($example, Dumper(@$to_dump), 'Subroutine call usage equivalent'); } + +my $out = DumperF { "arr: $_[0] str: $_[1]" } [qw(wut HALP)], "gnarl"; + +is($out, qq{arr: [\n "wut",\n "HALP"\n]\n str: "gnarl"\n}, 'DumperF works!'); diff --git a/t/sugar.t b/t/sugar.t index 8c1dba0..817f4ad 100644 --- a/t/sugar.t +++ b/t/sugar.t @@ -45,6 +45,13 @@ DWARN_CODEREF: { ok eq_array($foo, ['warn','friend']), 'Dwarn passes lists through correctly'; } +DWARNF: { + my @foo = DwarnF { "arr: $_[0] str: $_[1]" } [qw(wut HALP)], "gnarl"; + + is($warned_string, qq{arr: [\n "wut",\n "HALP"\n]\n str: "gnarl"\n}, 'DumperF works!'); + ok eq_array($foo[0], ['wut','HALP']) && $foo[1] eq 'gnarl', 'DwarnF passes lists through correctly'; +} + DWARNN: { my $loaded = eval { require Devel::ArgNames; 1 }; if ($loaded) {