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