doc for _only methods
[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 Dwarn_only DwarnS_only);
11
12
13 sub Dwarn { warn Data::Dumper::Concise::Dumper @_; @_ }
14
15 sub DwarnS ($) { warn Data::Dumper::Concise::Dumper $_[0]; $_[0] }
16
17 sub Dwarn_only (&@) {
18   my $only = shift;
19   warn Data::Dumper::Concise::Dumper $only->(@_);
20   @_
21 }
22
23 sub DwarnS_only (&$) {
24   my $only = shift;
25   warn Data::Dumper::Concise::Dumper do { local $_ = $_[0]; $only->($_[0]) };
26   $_[0]
27 }
28
29 =head1 NAME
30
31 Data::Dumper::Concise::Sugar - return Dwarn @return_value
32
33 =head1 SYNOPSIS
34
35   use Data::Dumper::Concise::Sugar;
36
37   return Dwarn some_call(...)
38
39 is equivalent to:
40
41   use Data::Dumper::Concise;
42
43   my @return = some_call(...);
44   warn Dumper(@return);
45   return @return;
46
47 but shorter. If you need to force scalar context on the value,
48
49   use Data::Dumper::Concise::Sugar;
50
51   return DwarnS some_call(...)
52
53 is equivalent to:
54
55   use Data::Dumper::Concise;
56
57   my $return = some_call(...);
58   warn Dumper($return);
59   return $return;
60
61 Sometimes you'll want to C<Dwarn> out part of a result, instead of the entire
62 thing; for that we have C<Dwarn_only>:
63
64   # Dwarn the TO_JSON of all the objects in the list
65   my @results = Dwarn_only { map $_->TO_JSON, @_ } some_call(...);
66
67 and C<DwarnS_only>:
68
69   # only Dwarn the first item
70   my $data = Dwarn_only { $_->[0] } [ some_call(...) ];
71
72 Another trick that is extremely useful when doing method chaining is the
73 following:
74
75   my $foo = Bar->new;
76   $foo->bar->baz->Data::Dumper::Concise::Sugar::DwarnS->biff;
77
78 which is the same as:
79
80   my $foo = Bar->new;
81   (DwarnS $foo->bar->baz)->biff;
82
83 =head1 DESCRIPTION
84
85   use Data::Dumper::Concise::Sugar;
86
87 will import Dwarn and DwarnS into your namespace. Using L<Exporter>, so see
88 its docs for ways to make it do something else.
89
90 =head2 Dwarn
91
92   sub Dwarn { warn Data::Dumper::Concise::Dumper @_; @_ }
93
94 =head2 DwarnS
95
96   sub DwarnS ($) { warn Data::Dumper::Concise::Dumper $_[0]; $_[0] }
97
98 =head2 Dwarn_only
99
100   sub Dwarn_only (&@) {
101     my $only = shift;
102     warn Data::Dumper::Concise::Dumper $only->(@_);
103     @_
104   }
105
106 =head2 DwarnS_only
107
108   sub DwarnS_only (&$) {
109     my $only = shift;
110     warn Data::Dumper::Concise::Dumper do { local $_ = $_[0]; $only->($_[0]) };
111     $_[0]
112   }
113
114 =head1 SEE ALSO
115
116 You probably want L<Devel::Dwarn>, it's the shorter name for this module.
117
118 =cut
119
120 1;