Create Ddie for die'ing output
[p5sagit/Data-Dumper-Concise.git] / lib / Data / Dumper / Concise / Sugar.pm
CommitLineData
ce1f3e8e 1package Data::Dumper::Concise::Sugar;
2
3use 5.006;
4
5use Exporter ();
6use Data::Dumper::Concise ();
7
8BEGIN { @ISA = qw(Exporter) }
9
2566c738 10@EXPORT = qw(
126b545b 11 $Dwarn $DwarnN Dwarn DwarnS DwarnL DwarnN DwarnF
2566c738 12 $Ddie $DdieN Ddie DdieS DdieL DdieN DdieD
13);
ce1f3e8e 14
e6746e64 15sub Dwarn { return DwarnL(@_) if wantarray; DwarnS($_[0]) }
16
7194c025 17our $Dwarn = \&Dwarn;
bef8cf5e 18our $DwarnN = \&DwarnN;
7194c025 19
e6746e64 20sub DwarnL { warn Data::Dumper::Concise::Dumper @_; @_ }
ce1f3e8e 21
22sub DwarnS ($) { warn Data::Dumper::Concise::Dumper $_[0]; $_[0] }
23
0deeb75f 24sub DwarnN ($) {
bef8cf5e 25 require Devel::ArgNames;
26 my $x = Devel::ArgNames::arg_names();
0deeb75f 27 warn(($x?$x:'(anon)') . ' => ' . Data::Dumper::Concise::Dumper $_[0]); $_[0]
28}
29
92264889 30sub DwarnF (&@) { my $c = shift; warn &Data::Dumper::Concise::DumperF($c, @_); @_ }
31
2566c738 32sub Ddie { DdieL(@_) if wantarray; DdieS($_[0]) }
33
34our $Ddie = \&Ddie;
35our $DdieN = \&DdieN;
36
37sub DdieL { die Data::Dumper::Concise::Dumper @_ }
38
39sub DdieS ($) { die Data::Dumper::Concise::Dumper $_[0] }
40
41sub DdieN ($) {
42 require Devel::ArgNames;
43 my $x = Devel::ArgNames::arg_names();
44 die(($x?$x:'(anon)') . ' => ' . Data::Dumper::Concise::Dumper $_[0]);
45}
46
ce1f3e8e 47=head1 NAME
48
49Data::Dumper::Concise::Sugar - return Dwarn @return_value
50
51=head1 SYNOPSIS
52
53 use Data::Dumper::Concise::Sugar;
54
55 return Dwarn some_call(...)
56
57is equivalent to:
58
59 use Data::Dumper::Concise;
60
13b908a8 61 if (wantarray) {
62 my @return = some_call(...);
63 warn Dumper(@return);
64 return @return;
65 } else {
66 my $return = some_call(...);
67 warn Dumper($return);
68 return $return;
69 }
ce1f3e8e 70
71but shorter. If you need to force scalar context on the value,
72
73 use Data::Dumper::Concise::Sugar;
74
75 return DwarnS some_call(...)
76
77is equivalent to:
78
79 use Data::Dumper::Concise;
80
81 my $return = some_call(...);
82 warn Dumper($return);
83 return $return;
84
13b908a8 85If you need to force list context on the value,
01223632 86
13b908a8 87 use Data::Dumper::Concise::Sugar;
01223632 88
13b908a8 89 return DwarnL some_call(...)
01223632 90
13b908a8 91is equivalent to:
92
93 use Data::Dumper::Concise;
94
95 my @return = some_call(...);
96 warn Dumper(@return);
97 return @return;
01223632 98
9469b5b0 99If you want to label your output, try DwarnN
100
101 use Data::Dumper::Concise::Sugar;
102
103 return DwarnN $foo
104
105is equivalent to:
106
107 use Data::Dumper::Concise;
108
109 my @return = some_call(...);
110 warn '$foo => ' . Dumper(@return);
111 return @return;
112
7194c025 113If you want to output a reference returned by a method easily, try $Dwarn
114
115 $foo->bar->{baz}->$Dwarn
116
117is equivalent to:
118
119 my $return = $foo->bar->{baz};
120 warn Dumper($return);
121 return $return;
122
92264889 123If you want to format the output of your data structures, try DwarnF
124
125 my ($a, $c) = DwarnF { "awesome: $_[0] not awesome: $_[1]" } $awesome, $cheesy;
126
127is equivalent to:
128
129 my @return = ($awesome, $cheesy);
130 warn DumperF { "awesome: $_[0] not awesome: $_[1]" } $awesome, $cheesy;
131 return @return;
132
2566c738 133If you want to immediately die after outputting the data structure, every
134Dwarn subroutine has a paired Ddie version, so just replace the warn with die.
135For example:
136
137 DdieL 'foo', { bar => 'baz' };
138
ce1f3e8e 139=head1 DESCRIPTION
140
141 use Data::Dumper::Concise::Sugar;
142
7194c025 143will import Dwarn, $Dwarn, DwarnL, DwarnN, and DwarnS into your namespace. Using
9469b5b0 144L<Exporter>, so see its docs for ways to make it do something else.
ce1f3e8e 145
146=head2 Dwarn
147
13b908a8 148 sub Dwarn { return DwarnL(@_) if wantarray; DwarnS($_[0]) }
149
7194c025 150=head2 $Dwarn
151
152 $Dwarn = \&Dwarn
153
bef8cf5e 154=head2 $DwarnN
155
156 $DwarnN = \&DwarnN
157
13b908a8 158=head2 DwarnL
159
ce1f3e8e 160 sub Dwarn { warn Data::Dumper::Concise::Dumper @_; @_ }
161
13b908a8 162=head2 DwarnS
ce1f3e8e 163
164 sub DwarnS ($) { warn Data::Dumper::Concise::Dumper $_[0]; $_[0] }
165
9469b5b0 166=head2 DwarnN
167
168 sub DwarnN { warn '$argname => ' . Data::Dumper::Concise::Dumper $_[0]; $_[0] }
169
bef8cf5e 170B<Note>: this requires L<Devel::ArgNames> to be installed.
171
92264889 172=head2 DwarnF
173
174 sub DwarnF (&@) { my $c = shift; warn &Data::Dumper::Concise::DumperF($c, @_); @_ }
175
13b908a8 176=head1 TIPS AND TRICKS
177
178=head2 global usage
179
180Instead of always just doing:
181
182 use Data::Dumper::Concise::Sugar;
183
184 Dwarn ...
185
186We tend to do:
187
188 perl -MData::Dumper::Concise::Sugar foo.pl
189
190(and then in the perl code:)
191
192 ::Dwarn ...
193
194That way, if you leave them in and run without the
195C<< use Data::Dumper::Concise::Sugar >> the program will fail to compile and
196you are less likely to check it in by accident. Furthmore it allows that
197much less friction to add debug messages.
198
199=head2 method chaining
200
201One trick which is useful when doing method chaining is the following:
202
203 my $foo = Bar->new;
204 $foo->bar->baz->Data::Dumper::Concise::Sugar::DwarnS->biff;
205
206which is the same as:
207
208 my $foo = Bar->new;
209 (DwarnS $foo->bar->baz)->biff;
210
dc06d25b 211=head1 SEE ALSO
212
213You probably want L<Devel::Dwarn>, it's the shorter name for this module.
214
215=cut
ce1f3e8e 216
2171;