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