add Dwarn_only and DwarnS_only
[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
8 BEGIN { @ISA = qw(Exporter) }
9
10 @EXPORT = qw(Dwarn DwarnS);
11
12 @EXPORT_OK = qw(Dwarn_only DwarnS_only);
13
14 sub Dwarn { warn Data::Dumper::Concise::Dumper @_; @_ }
15
16 sub DwarnS ($) { warn Data::Dumper::Concise::Dumper $_[0]; $_[0] }
17
18 sub Dwarn_only (&@) {
19   my $only = shift;
20   warn Data::Dumper::Concise::Dumper $only->(@_);
21   @_
22 }
23
24 sub DwarnS_only (&$) {
25   my $only = shift;
26   warn Data::Dumper::Concise::Dumper do { local $_ = $_[0]; $only->($_[0]) };
27   $_[0]
28 }
29
30 =head1 NAME
31
32 Data::Dumper::Concise::Sugar - return Dwarn @return_value
33
34 =head1 SYNOPSIS
35
36   use Data::Dumper::Concise::Sugar;
37
38   return Dwarn some_call(...)
39
40 is equivalent to:
41
42   use Data::Dumper::Concise;
43
44   my @return = some_call(...);
45   warn Dumper(@return);
46   return @return;
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 Another trick that is extremely useful when doing method chaining is the
63 following:
64
65   my $foo = Bar->new;
66   $foo->bar->baz->Data::Dumper::Concise::Sugar::DwarnS->biff;
67
68 which is the same as:
69
70   my $foo = Bar->new;
71   (DwarnS $foo->bar->baz)->biff;
72
73 =head1 DESCRIPTION
74
75   use Data::Dumper::Concise::Sugar;
76
77 will import Dwarn and DwarnS into your namespace. Using L<Exporter>, so see
78 its docs for ways to make it do something else.
79
80 =head2 Dwarn
81
82   sub Dwarn { warn Data::Dumper::Concise::Dumper @_; @_ }
83
84 =head3 DwarnS
85
86   sub DwarnS ($) { warn Data::Dumper::Concise::Dumper $_[0]; $_[0] }
87
88 =head1 SEE ALSO
89
90 You probably want L<Devel::Dwarn>, it's the shorter name for this module.
91
92 =cut
93
94 1;