File: typed_autovivification.pl

package info (click to toggle)
liblist-objects-withutils-perl 2.028003-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,276 kB
  • sloc: perl: 1,957; makefile: 17; sh: 6
file content (25 lines) | stat: -rw-r--r-- 866 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use strict; use warnings;

use List::Objects::WithUtils;

use List::Objects::Types -types;
use Types::Standard      -types;

# Consider a pattern like MooX::Role::POE::Emitter's registered event/session
# map; a given event name has a list of subscribed sessions, and we want
# constant-time access/deletion.
# A hash of hashes can help:
#   $registry->{$event}->{$session_id} = 1
# ... but I want some run-time checking to ensure the consistency of my
# hash while I'm abusing autovivification:
my $registry = hash_of TypedHash[Int];

# 'all' and 'foo' will be coerced to a List::Objects::WithUtils::Hash:
$registry->{ all }->{ 1234 } = 1;
$registry->{ foo }->{ 1234 } = 1;
use Data::Dumper;
print Dumper($registry), "\n\n";

# attempting to do something naughty will throw:
eval {; $registry->{ bar } = []; };
print "Attempting to add bad element produced:\n $@";