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