1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
package Data::Dumper::Concise::Sugar;
use 5.006;
our $VERSION = '2.023';
use Exporter ();
use Data::Dumper::Concise ();
BEGIN { @ISA = qw(Exporter) }
@EXPORT = qw(
$Dwarn $DwarnN Dwarn DwarnS DwarnL DwarnN DwarnF
$Ddie $DdieN Ddie DdieS DdieL DdieN DdieD
);
sub Dwarn { DwarnL(@_); return wantarray ? @_ : $_[0] }
our $Dwarn = \&Dwarn;
our $DwarnN = \&DwarnN;
sub DwarnL { warn Data::Dumper::Concise::Dumper @_; @_ }
sub DwarnS ($) { warn Data::Dumper::Concise::Dumper $_[0]; $_[0] }
sub DwarnN ($) {
require Devel::ArgNames;
my $x = Devel::ArgNames::arg_names();
warn(($x?$x:'(anon)') . ' => ' . Data::Dumper::Concise::Dumper $_[0]); $_[0]
}
sub DwarnF (&@) { my $c = shift; warn &Data::Dumper::Concise::DumperF($c, @_); @_ }
sub Ddie { DdieL(@_); return wantarray ? @_ : $_[0] }
our $Ddie = \&Ddie;
our $DdieN = \&DdieN;
sub DdieL { die Data::Dumper::Concise::Dumper @_ }
sub DdieS ($) { die Data::Dumper::Concise::Dumper $_[0] }
sub DdieN ($) {
require Devel::ArgNames;
my $x = Devel::ArgNames::arg_names();
die(($x?$x:'(anon)') . ' => ' . Data::Dumper::Concise::Dumper $_[0]);
}
=head1 NAME
Data::Dumper::Concise::Sugar - return Dwarn @return_value
=head1 SYNOPSIS
use Data::Dumper::Concise::Sugar;
return Dwarn some_call(...)
is equivalent to:
use Data::Dumper::Concise;
if (wantarray) {
my @return = some_call(...);
warn Dumper(@return);
return @return;
} else {
my $return = some_call(...);
warn Dumper($return);
return $return;
}
but shorter. If you need to force scalar context on the value,
use Data::Dumper::Concise::Sugar;
return DwarnS some_call(...)
is equivalent to:
use Data::Dumper::Concise;
my $return = some_call(...);
warn Dumper($return);
return $return;
If you need to force list context on the value,
use Data::Dumper::Concise::Sugar;
return DwarnL some_call(...)
is equivalent to:
use Data::Dumper::Concise;
my @return = some_call(...);
warn Dumper(@return);
return @return;
If you want to label your output, try DwarnN
use Data::Dumper::Concise::Sugar;
return DwarnN $foo
is equivalent to:
use Data::Dumper::Concise;
my @return = some_call(...);
warn '$foo => ' . Dumper(@return);
return @return;
If you want to output a reference returned by a method easily, try $Dwarn
$foo->bar->{baz}->$Dwarn
is equivalent to:
my $return = $foo->bar->{baz};
warn Dumper($return);
return $return;
If you want to format the output of your data structures, try DwarnF
my ($a, $c) = DwarnF { "awesome: $_[0] not awesome: $_[1]" } $awesome, $cheesy;
is equivalent to:
my @return = ($awesome, $cheesy);
warn DumperF { "awesome: $_[0] not awesome: $_[1]" } $awesome, $cheesy;
return @return;
If you want to immediately die after outputting the data structure, every
Dwarn subroutine has a paired Ddie version, so just replace the warn with die.
For example:
DdieL 'foo', { bar => 'baz' };
=head1 DESCRIPTION
use Data::Dumper::Concise::Sugar;
will import Dwarn, $Dwarn, DwarnL, DwarnN, and DwarnS into your namespace. Using
L<Exporter>, so see its docs for ways to make it do something else.
=head2 Dwarn
sub Dwarn { return DwarnL(@_) if wantarray; DwarnS($_[0]) }
=head2 $Dwarn
$Dwarn = \&Dwarn
=head2 $DwarnN
$DwarnN = \&DwarnN
=head2 DwarnL
sub Dwarn { warn Data::Dumper::Concise::Dumper @_; @_ }
=head2 DwarnS
sub DwarnS ($) { warn Data::Dumper::Concise::Dumper $_[0]; $_[0] }
=head2 DwarnN
sub DwarnN { warn '$argname => ' . Data::Dumper::Concise::Dumper $_[0]; $_[0] }
B<Note>: this requires L<Devel::ArgNames> to be installed.
=head2 DwarnF
sub DwarnF (&@) { my $c = shift; warn &Data::Dumper::Concise::DumperF($c, @_); @_ }
=head1 TIPS AND TRICKS
=head2 global usage
Instead of always just doing:
use Data::Dumper::Concise::Sugar;
Dwarn ...
We tend to do:
perl -MData::Dumper::Concise::Sugar foo.pl
(and then in the perl code:)
::Dwarn ...
That way, if you leave them in and run without the
C<< use Data::Dumper::Concise::Sugar >> the program will fail to compile and
you are less likely to check it in by accident. Furthmore it allows that
much less friction to add debug messages.
=head2 method chaining
One trick which is useful when doing method chaining is the following:
my $foo = Bar->new;
$foo->bar->baz->Data::Dumper::Concise::Sugar::DwarnS->biff;
which is the same as:
my $foo = Bar->new;
(DwarnS $foo->bar->baz)->biff;
=head1 SEE ALSO
You probably want L<Devel::Dwarn>, it's the shorter name for this module.
=cut
1;
|