| 1 | # Copyright © 2007-2009 Raphaël Hertzog <hertzog@debian.org> | ||||||||||||||||||||||||||||
| 2 | # Copyright © 2009, 2012-2019, 2021 Guillem Jover <guillem@debian.org> | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 5 | # it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||
| 6 | # the Free Software Foundation; either version 2 of the License, or | ||||||||||||||||||||||||||||
| 7 | # (at your option) any later version. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||
| 12 | # GNU General Public License for more details. | ||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||
| 14 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||
| 15 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =encoding utf8 | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | Dpkg::Control::HashCore::Tie - ties a Dpkg::Control::Hash object | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | This module provides a class that is used to tie a hash. | ||||||||||||||||||||||||||||
| 26 | It implements hash-like functions by normalizing the name of fields received | ||||||||||||||||||||||||||||
| 27 | in keys (using Dpkg::Control::Fields::field_capitalize()). | ||||||||||||||||||||||||||||
| 28 | It also stores the order in which fields have been added to be able | ||||||||||||||||||||||||||||
| 29 | to dump them in the same order. | ||||||||||||||||||||||||||||
| 30 | But the order information is stored in a parent object of type | ||||||||||||||||||||||||||||
| 31 | L<Dpkg::Control>. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | B<Note>: This is a private module, its API can change at any time. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | package Dpkg::Control::HashCore::Tie 0.01; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 15 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 15 | ▶ | use Dpkg::Control::FieldsCore; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 15 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 15 | ▶ | use Tie::Hash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 15 | ▶ | use parent -norequire, qw(Tie::ExtraHash); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # The class is implemented as an array ref with: | ||||||||||||||||||||||||||||
| 48 | # - $self->[0] is the real hash | ||||||||||||||||||||||||||||
| 49 | # - $self->[1] is a reference to the hash contained by the parent object | ||||||||||||||||||||||||||||
| 50 | # This reference bypasses the top-level scalar reference of a | ||||||||||||||||||||||||||||
| 51 | # Dpkg::Control::Hash, hence ensuring that reference gets DESTROYed | ||||||||||||||||||||||||||||
| 52 | # properly. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =over 4 | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =item $ctrl = Dpkg::Control::Hash->new($parent) | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Return a reference to a tied hash implementing storage of simple | ||||||||||||||||||||||||||||
| 61 | "field: value" mapping as used in many Debian-specific files. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 816 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 816 | my ($class, @args) = @_; | |||||||||||||||||||||||||||
| 67 | 816 | my $hash = {}; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 816 | tie %{$hash}, $class, @args; ## no critic (Miscellanea::ProhibitTies) | |||||||||||||||||||||||||||
| 70 | 816 | return $hash; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 816 | ▶ | sub TIEHASH { | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||
| 74 | 816 | my ($class, $parent) = @_; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 816 | ▶ | croak 'parent object must be Dpkg::Control::Hash' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$parent->isa("Dpkg::Control::HashCore") or $parent->isa("Dpkg::Control::Hash")
MC/DC0%0/2
$parent->isa("Dpkg::Control::HashCore") or $parent->isa("Dpkg::Control::Hash")
$parent->isa("Dpkg::Control::HashCore")
$parent->isa("Dpkg::Control::Hash")
Truth table33%1/3
A or B
A= $parent->isa("Dpkg::Control::HashCore")B= $parent->isa("Dpkg::Control::Hash")
| |||||||||||||||||||||||||||||
| 77 | if not $parent->isa('Dpkg::Control::HashCore') and | ||||||||||||||||||||||||||||
| 78 | not $parent->isa('Dpkg::Control::Hash'); | ||||||||||||||||||||||||||||
| 79 | 816 | return bless [ {}, $$parent ], $class; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 4742 | ▶ | sub FETCH { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 83 | 4742 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 4742 | $key = lc($key); | |||||||||||||||||||||||||||
| 86 | 4742 | ▶ | return $self->[0]->{$key} if exists $self->[0]->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 13 | return; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 6299 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 91 | 6299 | my ($self, $key, $value) = @_; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 6299 | $key = lc($key); | |||||||||||||||||||||||||||
| 94 | 6299 | ▶ | if (not exists $self->[0]->{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 4930 | push @{$self->[1]->{in_order}}, field_capitalize($key); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 6299 | $self->[0]->{$key} = $value; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 2397 | ▶ | sub EXISTS { | ||||||||||||||||||||||||||
|
Subroutine
EXISTS: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 101 | 2397 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 2397 | $key = lc($key); | |||||||||||||||||||||||||||
| 104 | 2397 | return exists $self->[0]->{$key}; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1 | ▶ | sub DELETE { | ||||||||||||||||||||||||||
|
Subroutine
DELETE: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 108 | 1 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 109 | 1 | my $parent = $self->[1]; | |||||||||||||||||||||||||||
| 110 | 1 | my $in_order = $parent->{in_order}; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | $key = lc($key); | |||||||||||||||||||||||||||
| 113 | 1 | ▶ | if (exists $self->[0]->{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 1 | delete $self->[0]->{$key}; | |||||||||||||||||||||||||||
| 115 | 1 | @{$in_order} = grep { lc ne $key } @{$in_order}; | |||||||||||||||||||||||||||
| 116 | 1 | return 1; | |||||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||||
| 118 | 0 | return 0; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 770 | ▶ | sub FIRSTKEY { | ||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 123 | 770 | my $self = shift; | |||||||||||||||||||||||||||
| 124 | 770 | my $parent = $self->[1]; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 770 | foreach my $key (@{$parent->{in_order}}) { | |||||||||||||||||||||||||||
| 127 | 770 | ▶ | return $key if exists $self->[0]->{lc $key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1427 | ▶ | sub NEXTKEY { | ||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 132 | 1427 | my ($self, $prev) = @_; | |||||||||||||||||||||||||||
| 133 | 1427 | my $parent = $self->[1]; | |||||||||||||||||||||||||||
| 134 | 1427 | my $found = 0; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1427 | foreach my $key (@{$parent->{in_order}}) { | |||||||||||||||||||||||||||
| 137 | 3106 | ▶ | if ($found) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 657 | ▶ | return $key if exists $self->[0]->{lc $key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | } else { | ||||||||||||||||||||||||||||
| 140 | 2449 | ▶ | $found = 1 if $key eq $prev; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 770 | return; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =back | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head1 CHANGES | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 Version 0.xx | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | This is a private module. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 15 | 1; | |||||||||||||||||||||||||||