Add DwarnN
[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 =head1 DESCRIPTION
77
78   use Data::Dumper::Concise::Sugar;
79
80 will import Dwarn, DwarnL, and DwarnS into your namespace. Using L<Exporter>, so see
81 its docs for ways to make it do something else.
82
83 =head2 Dwarn
84
85   sub Dwarn { return DwarnL(@_) if wantarray; DwarnS($_[0]) }
86
87 =head2 DwarnL
88
89   sub Dwarn { warn Data::Dumper::Concise::Dumper @_; @_ }
90
91 =head2 DwarnS
92
93   sub DwarnS ($) { warn Data::Dumper::Concise::Dumper $_[0]; $_[0] }
94
95 =head1 TIPS AND TRICKS
96
97 =head2 global usage
98
99 Instead of always just doing:
100
101   use Data::Dumper::Concise::Sugar;
102
103   Dwarn ...
104
105 We tend to do:
106
107   perl -MData::Dumper::Concise::Sugar foo.pl
108
109 (and then in the perl code:)
110
111   ::Dwarn ...
112
113 That way, if you leave them in and run without the
114 C<< use Data::Dumper::Concise::Sugar >> the program will fail to compile and
115 you are less likely to check it in by accident.  Furthmore it allows that
116 much less friction to add debug messages.
117
118 =head2 method chaining
119
120 One trick which is useful when doing method chaining is the following:
121
122   my $foo = Bar->new;
123   $foo->bar->baz->Data::Dumper::Concise::Sugar::DwarnS->biff;
124
125 which is the same as:
126
127   my $foo = Bar->new;
128   (DwarnS $foo->bar->baz)->biff;
129
130 =head1 SEE ALSO
131
132 You probably want L<Devel::Dwarn>, it's the shorter name for this module.
133
134 =cut
135
136 1;