From 622abfca2c8e29d7d6118971fd35f5da8875fe43 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 1 Aug 2024 01:28:27 -0400 Subject: [PATCH] repr: New utility function to make it easier to dump values I find Data::Dumper to be awkward to use. This function wraps it so that I don't have to keep looking up the perldoc. --- ddclient.in | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ddclient.in b/ddclient.in index f37b139..b691937 100755 --- a/ddclient.in +++ b/ddclient.in @@ -159,6 +159,14 @@ my $daemon; # Control how many times warning message logged for invalid IP addresses my (%warned_ip, %warned_ipv4, %warned_ipv6); +sub repr { + my $vals = @_ % 2 ? [shift] : []; + my %opts = @_; + my $d = Data::Dumper->new($vals)->Sortkeys(1)->Terse(!exists($opts{Names}))->Useqq(1); + $d->$_($opts{$_}) for keys(%opts); + return $d->Dump(); +} + sub T_ANY { 'any' } sub T_STRING { 'string' } sub T_EMAIL { 'e-mail address' } @@ -3359,10 +3367,9 @@ sub group_hosts_by { @attrs = sort(keys(%attrs)); my %groups; my %cfgs; - my $d = Data::Dumper->new([])->Indent(0)->Sortkeys(1)->Terse(1)->Useqq(1); for my $h (@$hosts) { my %cfg = map({ ($_ => $config{$h}{$_}); } grep(exists($config{$h}{$_}), @attrs)); - my $sig = $d->Reset()->Values([\%cfg])->Dump(); + my $sig = repr(\%cfg, Indent => 0); push(@{$groups{$sig}}, $h); $cfgs{$sig} = \%cfg; }