Hash1.pm

filter: Statementstmt 100% 45 / 45 Branchbran 100% 10 / 10 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 100% 76 / 76 CC 11 SCAR 19.6
CC
11
Cov
100%
sort_hash
11.0
BEGIN
1.0
BEGIN
1.0
CRAP 7.1
« Hash.pm ↑ Summary
1 package Sort::Hash1;
2 {
3 1 $Sort::Hash1::VERSION = '2.05';
4 }
5 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Try::Tiny 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Scalar::Util 1.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our @EXPORT = qw( sort_hash ); # symbols to export on request
12  
13 =head1 NAME
14  
15 Sort::Hash1 Version1 of SortHash
16  
17 =head1 VERSION
18  
19 1.042
20  
21 =head1 SYNOPSIS
22  
23 Sort::Hash1 is included in the distribution for backwards compatibility. If you used Sort::Hash and don't want to fix the code right now, use Sort::Hash1. If you are writing or maintaining your code use Sort::Hash instead.
24  
25 =head2 sort_hash
26  
27 The interface to sort_hash changed from version 1 to version 2. Instead of taking a hash of values it now takes a list in which the first item must be a reference to the hash to sort. Sort::Hash1 will be removed from the distribution in the future, updating your code is easy:
28  
29 sort_hash( desc => 1 , alpha => 1, %somehash );
30 becomes
31 sort_hash( \%somehash, 'desc', 'alpha' );
32  
33 =cut
34  
35 9 ▶sub sort_hash {
Subroutine sort_hash: called CC=11 SCAR=24.0
Pod documented
36 9 my %H = @_;
37 9 my @sorted = ();
38 9 ▶ my $direction = delete $H{direction} || 'asc';
Condition100%2/2
delete $H{'direction'} || 'asc'
l!l
18
MC/DC100%1/1
delete $H{'direction'} || 'asc'
delete $H{'direction'}
Truth table100%2/2
A
A= delete $H{'direction'}
Aresult
11
00
39 9 ▶ my $alpha = delete $H{alpha} || 0;
Condition100%2/2
delete $H{'alpha'} || 0
l!l
45
MC/DC100%1/1
delete $H{'alpha'} || 0
delete $H{'alpha'}
Truth table100%2/2
A
A= delete $H{'alpha'}
Aresult
11
00
40 9 ▶ my $strictalpha = delete $H{strictalpha} || 0;
Condition100%2/2
delete $H{'strictalpha'} || 0
l!l
18
MC/DC100%1/1
delete $H{'strictalpha'} || 0
delete $H{'strictalpha'}
Truth table100%2/2
A
A= delete $H{'strictalpha'}
Aresult
11
00
41 9 ▶ my $numeric = delete $H{numeric} || 1;
Condition100%2/2
delete $H{'numeric'} || 1
l!l
18
MC/DC100%1/1
delete $H{'numeric'} || 1
delete $H{'numeric'}
Truth table100%2/2
A
A= delete $H{'numeric'}
Aresult
11
00
42 9 ▶ if ( defined $H{hashref} ) { %H = %{ $H{hashref} } }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $H{"hashref"}) 1 8 9
43 9 ▶ if ($strictalpha) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strictalpha) 1 8 9
44 1 $alpha = 1;
45 1 for ( values %H ) {
46 6 ▶ if (Scalar::Util::looks_like_number($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Scalar::Util::looks_like_number($_)) 1 5 6
47 1 warn 'Attempt to Sort Numeric Value in Strict Alpha Sort';
48 1 return undef }
49 }
50 }
51 8 ▶ if ($alpha) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alpha) { } 4 4 8
52 4 @sorted = ( sort { lc $H{$a} cmp lc $H{$b} } keys %H ) ;
53 }
54 else {
55 4 ▶ try { @sorted = ( sort { $H{$a} <=> $H{$b} } keys %H ) ;}
Subroutine __ANON__: called CC=1 SCAR=0.0
56 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 1 warn 'Attempt to Sort non-Numeric values in a Numeric Sort';
58 1 return undef ; }
59 4 }
60 8 ▶ if ( lc($direction) eq 'desc' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $direction eq 'desc') { } 1 7 8
61 1 return reverse @sorted;
62 }
63 7 else { return @sorted; }
64 }
65  
66 =head1 AUTHOR
67  
68 John Karr, C<< <brainbuz at brainbuz.org> >>
69  
70 =head1 BUGS
71  
72 Please report any bugs or feature requests via the BitBucket issue tracker at
73 L<https://bitbucket.org/brainbuz/sort-hash/issues>. I will be
74 notified, and then you'll automatically be notified of progress on
75 your bug as I make changes.
76  
77 =head1 SUPPORT
78  
79 You can find documentation for this module with the perldoc command.
80  
81 You can also look for information at: The documentation for the
82 sort command in the Perl documentation.
83  
84 =head1 LICENSE AND COPYRIGHT
85  
86 Copyright 2014 John Karr.
87  
88 This program is free software; you can redistribute it and/or modify
89 it under the terms of the GNU General Public License as published by
90 the Free Software Foundation; version 3 or at your option
91 any later version.
92  
93 This program is distributed in the hope that it will be useful,
94 but WITHOUT ANY WARRANTY; without even the implied warranty of
95 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
96 GNU General Public License for more details.
97  
98 A copy of the GNU General Public License is available in the source tree;
99 if not, write to the Free Software Foundation, Inc.,
100 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
101  
102 =cut
103  
104 1 1;
« Hash.pm ↑ Summary