Skip to content

Data::Dumper: Show only existing hash keys returned by Sortkeys(sub{...}) #21050

@jimav

Description

@jimav

This is an enhancement suggestion.

Currently a hash member is shown for every key returned by a Sortkeys sub even if there is no such member. The output shows non-existent members as existing with an undef value.

It would be nice (and arguably more correct) to omit hash members which don't actually exist.

To get a correct display, one currently must do something like

Data::Dumper->new([$href])->Sortkeys(sub{ [ grep{ exists($_[0]->{$_}) } qw/all possible keys/]}->Dump

Whereas it would be nice to be able to just say

Data::Dumper->new([$href])->Sortkeys(sub{[qw/all possible keys/]}->Dump

Demo program:

#!/usr/bin/perl
use strict; use warnings; use feature qw/say/;
use Data::Dumper;

my $href1 = { aaa => 100 };
my $href2 = { aaa => 100, OPTIONAL => undef };
say Data::Dumper->new([$href1, $href2],['href1','href2'])->Sortkeys(sub{[qw/OPTIONAL aaa bbb/]})->Dump;

Current results:

$href1 = {
           'OPTIONAL' => undef,
           'aaa' => 100,
           'bbb' => undef
         };
$href2 = {
           'OPTIONAL' => undef,
           'aaa' => 100,
           'bbb' => undef
         };

Desired results:

$href1 = {
           'aaa' => 100,
           'bbb' => undef
         };
$href2 = {
           'OPTIONAL' => undef,
           'aaa' => 100,
           'bbb' => undef
         };

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions