Skip to content

Commit

Permalink
Use Data::Dumper as an object.
Browse files Browse the repository at this point in the history
Instead of just setting global Dumper variables, use it as an object so
it doesn't affect any calling code.

Fixes abw#136
  • Loading branch information
dnfm committed Apr 27, 2021
1 parent 3d69700 commit fd38ada
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions lib/Template/Plugin/Dumper.pm
Expand Up @@ -40,39 +40,44 @@ our $AUTOLOAD;

sub new {
my ($class, $context, $params) = @_;
my ($key, $val);
$params ||= { };


foreach my $arg (@DUMPER_ARGS) {
no strict 'refs';
if (defined ($val = $params->{ lc $arg })
or defined ($val = $params->{ $arg })) {
${"Data\::Dumper\::$arg"} = $val;
}
}

bless {
_CONTEXT => $context,
params => $params || {},
}, $class;
}

sub dump {
sub get_dump_obj {
my $self = shift;
my $content = Dumper @_;
return $content;

my $dumper_obj = Data::Dumper->new( \@_ );

my $params = $self->{ params };

foreach my $arg ( @DUMPER_ARGS ) {
my $val = exists $params->{ lc $arg } ? $params->{ lc $arg }
: $params->{ $arg };

$dumper_obj->$arg( $val ) if defined $val;
}

return $dumper_obj;
}

sub dump { scalar shift->get_dump_obj( @_ )->Dump() }

sub dump_html {
my $self = shift;
my $content = Dumper @_;

my $content = $self->dump( @_ );

for ($content) {
s/&/&/g;
s/</&lt;/g;
s/>/&gt;/g;
s/\n/<br>\n/g;
}

return $content;
}

Expand Down

0 comments on commit fd38ada

Please sign in to comment.