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.
This commit is contained in:
Richard Hansen 2024-08-01 01:28:27 -04:00
parent 4f369a3b0b
commit 622abfca2c

View file

@ -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;
}