Data-Dumper-Simple
view release on metacpan or search on metacpan
lib/Data/Dumper/Simple.pm view on Meta::CPAN
my $arguments = shift;
my $sigils = '@%&';
my @raw_var_names =
map { _strip_whitespace($_) }
split /(?:,|=>)/ => $arguments;
my @raw_escaped = @raw_var_names;
my $varnames = join ' ' => map {
s/(\\)?[$sigils]/$1 ? '$' : '*'/ge;
s/\\//g;
$_
} # turn @array into => [$*]array
@raw_var_names;
my $escaped_vars =
join ', ' => map { s/\\\$/\$/g; $_ } # do not take a reference to a scalar
map { s/(?<!\\)(?=[$sigils])/\\/g; $_ } # take references to all else
@raw_escaped;
return ( $escaped_vars, $varnames );
}
sub _strip_whitespace {
$_[0] =~ s/\s//g;
return $_[0];
}
1;
__END__
=head1 NAME
Data::Dumper::Simple - Easily dump variables with names
=head1 SYNOPSIS
use Data::Dumper::Simple;
warn Dumper($scalar, @array, %hash);
warn Dumper($scalar, \@array, \%hash);
warn Dumper $scalar, @array, %hash;
=head1 ABSTRACT
This module allow the user to dump variables in a Data::Dumper format.
Unlike the default behavior of Data::Dumper, the variables are named
(instead of $VAR1, $VAR2, etc.) Data::Dumper provides an extended
interface that allows the programmer to name the variables, but this
interface requires a lot of typing and is prone to tyops (sic). This
module fixes that.
=head1 DESCRIPTION
C<Data::Dumper::Simple> is actually a source filter that replaces all instances
of C<Dumper($some, @args)> in your code with a call to
C<Data::Dumper-E<gt>Dump()>. You can use the one function provided to make
dumping variables for debugging a trivial task.
Note that this is primarily a debugging tool. C<Data::Dumper> offers a bit
more than that, so don't expect this module to be more than it is.
Note that if you strongly object to source filters, I've also released
L<Data::Dumper::Names>. It does what this module does by it uses L<PadWalker>
instead of a source filter. Unfortunately, it has a few limitations and is not
as powerful as this module. Think of L<Data::Dumper::Names> as a "proof of
concept".
=head2 The Problem
Frequently, we use C<Data::Dumper> to dump out some variables while debugging.
When this happens, we often do this:
use Data::Dumper;
warn Dumper($foo, $bar, $baz);
And we get simple output like:
$VAR1 = 3;
$VAR2 = 2;
$VAR3 = 1;
While this is usually what we want, this can be confusing if we forget which
variable corresponds to which variable printed. To get around this, there is
an extended interface to C<Data::Dumper>:
warn Data::Dumper->Dump(
[$foo, $bar, $baz],
[qw/*foo *bar *baz/]
);
This provides much more useful output.
$foo = 3;
$bar = 2;
$baz = 1;
(There's more control over the output than what I've shown.)
You can even use this to output more complex data structures:
warn Data::Dumper->Dump(
[$foo, \@array],
[qw/*foo *array/]
);
And get something like this:
$foo = 3;
@array = (
8,
'Ovid'
);
Unfortunately, this can involve a lot of annoying typing.
warn Data::Dumper->Dump(
[$foo, \%this, \@array, \%that],
[qw/*foo *that *array *this/]
);
You'll also notice a typo in the second array ref which can cause great
confusion while debugging.
( run in 2.997 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )