Instantly share code, notes, and snippets.

@andrewsolomon /rmap.pl Secret
Created Nov 12, 2018

Embed
What would you like to do?
#!/usr/bin/env perl
use Modern::Perl;
use Data::Dump qw/pp/;
use Data::Rmap qw/rmap_hash/;
my $world = [
{
name => 'USA',
type => 'Country',
territories => [
{
name => 'Washington, D.C',
type => 'District',
streets => [
{
type => 'Avenue',
name => 'Pennsylvania',
properties => [
{
type => 'House',
colour => 'White',
inhabitants => [
{
name => 'Donald',
age => 3,
behaviour => 'naughty',
stocking => [],
},
],
},
],
},
],
},
],
},
{
name => 'Jungle',
type => 'Company',
staff => [
{
name => 'Jeff',
age => 7,
behaviour => 'naughty',
stocking => [],
role => 'CEO',
}
],
},
{
name => 'Monaco',
type => 'Tax Haven',
evaders => [
{
name => 'Ebenezer "Bono" Scrooge',
age => 12,
behaviour => 'nice',
stocking => [],
role => 'Philanthropist',
}
],
},
];
sub they_deserve_anything_at_all {
my $person = shift;
foreach my $key (qw/name age behaviour stocking/) {
return unless exists($person->{$key});
}
return unless ref($person->{stocking}) eq 'ARRAY';
return unless $person->{age} < 16;
return 1;
}
sub what_they_deserve {
my $person = shift;
return $person->{behaviour} eq 'nice' ? 'lollipop' : 'coal';
}
rmap_hash {
# Data::Rmap::cut if $_->{type} && $_->{type} =~ m{^(Company|Tax Haven)$};
push(@{$_->{stocking}}, what_they_deserve($_))
if they_deserve_anything_at_all($_)
} $world;
say pp($world);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment