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