1 package Data::Dumper::Concise::Sugar;
6 use Data::Dumper::Concise ();
8 BEGIN { @ISA = qw(Exporter) }
11 $Dwarn $DwarnN Dwarn DwarnS DwarnL DwarnN DwarnF
12 $Ddie $DdieN Ddie DdieS DdieL DdieN DdieD
15 sub Dwarn { return DwarnL(@_) if wantarray; DwarnS($_[0]) }
18 our $DwarnN = \&DwarnN;
20 sub DwarnL { warn Data::Dumper::Concise::Dumper @_; @_ }
22 sub DwarnS ($) { warn Data::Dumper::Concise::Dumper $_[0]; $_[0] }
25 require Devel::ArgNames;
26 my $x = Devel::ArgNames::arg_names();
27 warn(($x?$x:'(anon)') . ' => ' . Data::Dumper::Concise::Dumper $_[0]); $_[0]
30 sub DwarnF (&@) { my $c = shift; warn &Data::Dumper::Concise::DumperF($c, @_); @_ }
32 sub Ddie { DdieL(@_) if wantarray; DdieS($_[0]) }
37 sub DdieL { die Data::Dumper::Concise::Dumper @_ }
39 sub DdieS ($) { die Data::Dumper::Concise::Dumper $_[0] }
42 require Devel::ArgNames;
43 my $x = Devel::ArgNames::arg_names();
44 die(($x?$x:'(anon)') . ' => ' . Data::Dumper::Concise::Dumper $_[0]);
49 Data::Dumper::Concise::Sugar - return Dwarn @return_value
53 use Data::Dumper::Concise::Sugar;
55 return Dwarn some_call(...)
59 use Data::Dumper::Concise;
62 my @return = some_call(...);
66 my $return = some_call(...);
71 but shorter. If you need to force scalar context on the value,
73 use Data::Dumper::Concise::Sugar;
75 return DwarnS some_call(...)
79 use Data::Dumper::Concise;
81 my $return = some_call(...);
85 If you need to force list context on the value,
87 use Data::Dumper::Concise::Sugar;
89 return DwarnL some_call(...)
93 use Data::Dumper::Concise;
95 my @return = some_call(...);
99 If you want to label your output, try DwarnN
101 use Data::Dumper::Concise::Sugar;
107 use Data::Dumper::Concise;
109 my @return = some_call(...);
110 warn '$foo => ' . Dumper(@return);
113 If you want to output a reference returned by a method easily, try $Dwarn
115 $foo->bar->{baz}->$Dwarn
119 my $return = $foo->bar->{baz};
120 warn Dumper($return);
123 If you want to format the output of your data structures, try DwarnF
125 my ($a, $c) = DwarnF { "awesome: $_[0] not awesome: $_[1]" } $awesome, $cheesy;
129 my @return = ($awesome, $cheesy);
130 warn DumperF { "awesome: $_[0] not awesome: $_[1]" } $awesome, $cheesy;
133 If you want to immediately die after outputting the data structure, every
134 Dwarn subroutine has a paired Ddie version, so just replace the warn with die.
137 DdieL 'foo', { bar => 'baz' };
141 use Data::Dumper::Concise::Sugar;
143 will import Dwarn, $Dwarn, DwarnL, DwarnN, and DwarnS into your namespace. Using
144 L<Exporter>, so see its docs for ways to make it do something else.
148 sub Dwarn { return DwarnL(@_) if wantarray; DwarnS($_[0]) }
160 sub Dwarn { warn Data::Dumper::Concise::Dumper @_; @_ }
164 sub DwarnS ($) { warn Data::Dumper::Concise::Dumper $_[0]; $_[0] }
168 sub DwarnN { warn '$argname => ' . Data::Dumper::Concise::Dumper $_[0]; $_[0] }
170 B<Note>: this requires L<Devel::ArgNames> to be installed.
174 sub DwarnF (&@) { my $c = shift; warn &Data::Dumper::Concise::DumperF($c, @_); @_ }
176 =head1 TIPS AND TRICKS
180 Instead of always just doing:
182 use Data::Dumper::Concise::Sugar;
188 perl -MData::Dumper::Concise::Sugar foo.pl
190 (and then in the perl code:)
194 That way, if you leave them in and run without the
195 C<< use Data::Dumper::Concise::Sugar >> the program will fail to compile and
196 you are less likely to check it in by accident. Furthmore it allows that
197 much less friction to add debug messages.
199 =head2 method chaining
201 One trick which is useful when doing method chaining is the following:
204 $foo->bar->baz->Data::Dumper::Concise::Sugar::DwarnS->biff;
206 which is the same as:
209 (DwarnS $foo->bar->baz)->biff;
213 You probably want L<Devel::Dwarn>, it's the shorter name for this module.