File Coverage

blib/lib/SHARYANTO/Hash/Util.pm
Criterion Covered Total %
statement 15 15 100.0
branch 4 4 100.0
condition n/a
subroutine 5 5 100.0
pod 2 2 100.0
total 26 26 100.0


line stmt bran cond sub pod time code
1             package SHARYANTO::Hash::Util;
2              
3             our $DATE = '2015-09-04'; # DATE
4             our $VERSION = '0.77'; # VERSION
5              
6 1     1   23072 use 5.010;
  1         4  
7 1     1   5 use strict;
  1         2  
  1         19  
8 1     1   6 use warnings;
  1         2  
  1         142  
9              
10             require Exporter;
11             our @ISA = qw(Exporter);
12             our @EXPORT_OK = qw(rename_key replace_hash_content);
13              
14             sub rename_key {
15 3     3 1 1627 my ($h, $okey, $nkey) = @_;
16 3 100       20 die unless exists $h->{$okey};
17 2 100       12 die if exists $h->{$nkey};
18 1         4 $h->{$nkey} = delete $h->{$okey};
19             }
20              
21             sub replace_hash_content {
22 1     1 1 1899 my $hashref = shift;
23 1         3 %$hashref = @_;
24 1         2 $hashref;
25             }
26              
27             1;
28             # ABSTRACT: Hash utilities
29              
30             __END__
31              
32             =pod
33              
34             =encoding UTF-8
35              
36             =head1 NAME
37              
38             SHARYANTO::Hash::Util - Hash utilities
39              
40             =head1 VERSION
41              
42             This document describes version 0.77 of SHARYANTO::Hash::Util (from Perl distribution SHARYANTO-Utils), released on 2015-09-04.
43              
44             =head1 SYNOPSIS
45              
46             use SHARYANTO::Hash::Util qw(rename_key);
47             my %h = (a=>1, b=>2);
48             rename_key(\%h, "a", "alpha"); # %h = (alpha=>1, b=>2)
49              
50             =head1 FUNCTIONS
51              
52             =head2 rename_key(\%hash, $old_key, $new_key)
53              
54             Rename key. This is basically C<< $hash{$new_key} = delete $hash{$old_key} >>
55             with a couple of additional checks. It is a shortcut for:
56              
57             die unless exists $hash{$old_key};
58             die if exists $hash{$new_key};
59             $hash{$new_key} = delete $hash{$old_key};
60              
61             =head2 replace_hash_content($hashref, @pairs) => $hashref
62              
63             Replace content in <$hashref> with @list. Return C<$hashref>. Do not create a
64             new hashref object (i.e. it is different from: C<< $hashref = {new=>"content"}
65             >>).
66              
67             Do not use this function. In Perl you can just use: C<< %$hashref = @pairs >>. I
68             put the function here for reminder.
69              
70             =head1 SEE ALSO
71              
72             L<SHARYANTO>
73              
74             =head1 HOMEPAGE
75              
76             Please visit the project's homepage at L<https://metacpan.org/release/SHARYANTO-Utils>.
77              
78             =head1 SOURCE
79              
80             Source repository is at L<https://github.com/perlancar/perl-SHARYANTO-Utils>.
81              
82             =head1 BUGS
83              
84             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=SHARYANTO-Utils>
85              
86             When submitting a bug or request, please include a test-file or a
87             patch to an existing test-file that illustrates the bug or desired
88             feature.
89              
90             =head1 AUTHOR
91              
92             perlancar <perlancar@cpan.org>
93              
94             =head1 COPYRIGHT AND LICENSE
95              
96             This software is copyright (c) 2015 by perlancar@cpan.org.
97              
98             This is free software; you can redistribute it and/or modify it under
99             the same terms as the Perl 5 programming language system itself.
100              
101             =cut