This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Module-CoreList prepared for v5.19.8
[perl5.git] / dist / Module-CoreList / lib / Module / CoreList / TieHashDelta.pm
CommitLineData
a272bf38
DL
1# For internal Module::CoreList use only.
2package Module::CoreList::TieHashDelta;
3use strict;
4use vars qw($VERSION);
5
365f8c3e 6$VERSION = "3.04";
a272bf38
DL
7
8sub TIEHASH {
9 my ($class, $changed, $removed, $parent) = @_;
10
11 return bless {
12 changed => $changed,
13 removed => $removed,
14 parent => $parent,
15 keys_inflated => 0,
16 }, $class;
17}
18
19sub FETCH {
20 my ($self, $key) = @_;
21
22 if (exists $self->{changed}{$key}) {
23 return $self->{changed}{$key};
24 } elsif (exists $self->{removed}{$key}) {
25 return undef;
26 } elsif (defined $self->{parent}) {
27 return $self->{parent}{$key};
28 }
29 return undef;
30}
31
32sub EXISTS {
33 my ($self, $key) = @_;
34
35 if (exists $self->{changed}{$key}) {
36 return 1;
37 } elsif (exists $self->{removed}{$key}) {
38 return '';
39 } elsif (defined $self->{parent}) {
40 return exists $self->{parent}{$key};
41 }
42 return '';
43}
44
45sub FIRSTKEY {
46 my ($self) = @_;
47
48 if (not $self->{keys_inflated}) {
49 # This inflates the whole set of hashes... Somewhat expensive, but saves
50 # many tied hash calls later.
51 my @parent_keys;
52 if (defined $self->{parent}) {
53 @parent_keys = keys %{$self->{parent}};
54 }
55
56 @parent_keys = grep !exists $self->{removed}{$_}, @parent_keys;
57 for my $key (@parent_keys) {
58 next if exists $self->{changed}->{$key};
59 $self->{changed}{$key} = $self->{parent}{$key};
60 }
61
62 $self->{keys_inflated} = 1;
63 }
64
65 keys %{$self->{changed}}; # reset each
66 $self->NEXTKEY;
67}
68
69sub NEXTKEY {
70 my ($self) = @_;
711703f9 71 each %{$self->{changed}};
a272bf38
DL
72}
73
741;