File Coverage

blib/lib/Hash/Case.pm
Criterion Covered Total %
statement 32 43 74.4
branch 7 14 50.0
condition n/a
subroutine 9 11 81.8
pod 3 5 60.0
total 51 73 69.8


line stmt bran cond sub pod time code
1             # This code is part of Perl distribution Hash-Case version 1.07.
2             # The POD got stripped from this file by OODoc version 3.06.
3             # For contributors see file ChangeLog.
4              
5             # This software is copyright (c) 2002-2026 by Mark Overmeer.
6              
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10              
11              
12             package Hash::Case;{
13             our $VERSION = '1.07';
14             }
15              
16              
17 4     4   3406 use Tie::Hash; # Tie::StdHash is a hidden package inside this :-(
  4         4380  
  4         193  
18 4     4   29 use base 'Tie::StdHash';
  4         8  
  4         2249  
19              
20 4     4   33 use warnings;
  4         16  
  4         354  
21 4     4   33 use strict;
  4         9  
  4         196  
22              
23 4     4   25 use Carp qw/croak/;
  4         7  
  4         2559  
24              
25             #--------------------
26              
27             sub TIEHASH(@)
28 13     13   1055815 { my $class = shift;
29 13 100       109 my $to = @_ % 2 ? shift : undef;
30 13         60 my %opts = (@_, add => $to);
31 13         82 (bless {}, $class)->init( \%opts );
32             }
33              
34             # Used for case-insensitive hashes which do not need more than
35             # one hash.
36             sub native_init($)
37 13     13 0 34 { my ($self, $args) = @_;
38 13         31 my $add = delete $args->{add};
39              
40 13 100       77 if(!$add) { ; }
    100          
    50          
41 4         40 elsif(ref $add eq 'ARRAY') { $self->addPairs(@$add) }
42 4         34 elsif(ref $add eq 'HASH') { $self->addHashData($add) }
43 0         0 else { croak "cannot initialize the native hash this way" }
44              
45 13         50 $self;
46             }
47              
48             # Used for case-insensitive hashes which are implemented around
49             # an existing hash.
50             sub wrapper_init($)
51 0     0 0 0 { my ($self, $args) = @_;
52 0         0 my $add = delete $args->{add};
53              
54 0 0       0 if(!$add) { ; }
    0          
    0          
55 0         0 elsif(ref $add eq 'ARRAY') { $self->addPairs(@$add) }
56 0         0 elsif(ref $add eq 'HASH') { $self->setHash($add) }
57 0         0 else { croak "cannot initialize a wrapping hash this way" }
58              
59 0         0 $self;
60             }
61              
62             #-----------
63              
64             sub addPairs(@)
65 4     4 1 10 { my $self = shift;
66 4         27 $self->STORE(shift, shift) while @_;
67 4         9 $self;
68             }
69              
70              
71             sub addHashData($)
72 4     4 1 12 { my ($self, $data) = @_;
73 4         39 while(my ($k, $v) = each %$data) { $self->STORE($k, $v) }
  8         26  
74 4         10 $self;
75             }
76              
77              
78             sub setHash($)
79 0     0 1   { my ($self, $hash) = @_; # the native implementation is the default.
80 0           %$self = %$hash;
81 0           $self;
82             }
83              
84             1;