Hash.pm

filter: Statementstmt 97.4% 77 / 79 Branchbran 94.1% 32 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 96.7% 120 / 124 CC 20 SCAR 24.7
CC
20
Cov
96%
sort_hash
18.0
__ANON__
3.0
BEGIN
1.0
CRAP 11.8
↑ Summary Hash1.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Sort::Hash;
5 {
6 1 $Sort::Hash::VERSION = '2.05';
7 }
8 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Try::Tiny 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Scalar::Util 1.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our @EXPORT = qw( sort_hash ); # symbols to export on request
14  
15 # ABSTRACT: Sort the keys of a Hash into an Array.
16  
17 =pod
18  
19 =head1 NAME
20  
21 Sort::Hash - get the keys to a hashref sorted by their values.
22  
23 =head1 VERSION
24  
25 version 2.05
26  
27 =head1 SYNOPSIS
28  
29 Hash::Sort is a convenience for returning the keys of a hashref
30 sorted by their values. Numeric and alphanumeric sorting are supported,
31 the sort may be either Ascending or Descending.
32  
33 use Sort::Hash;
34 my @sorted = sort_hash( \%Hash );
35  
36 This does exactly the same as:
37  
38 my @sorted = ( sort { $Hash{$a} <=> $Hash{$b} } keys %Hash ) ;
39  
40 =head1 DESCRIPTION
41  
42 Get the keys to a hashref sorted by their values.
43  
44 =head1 Methods Exported
45  
46 =head2 sort_hash
47  
48 Return a sorted array containing the keys of a hash.
49  
50 =head3 Options to sort_hash
51  
52 nofatal warn and return an empty list instead of dying on
53 invalid sort (default behaviour)
54 silent like nofatal but doesn't emit warnings either
55 noempty if the hashref is empty treat it as an error
56 instead of returning an empty list ()
57 desc sort descending instead of ascending
58 asc ascending sort is the default but you can specify it
59 alpha sort alpha (treats numbers as text)
60 strictalpha sort alpha but refuse to sort numbers as text
61 numeric sort as numbers, default is numeric
62  
63 The arguments may be passed in any order.
64  
65 sort_hash( 'strictalpha', 'desc', $hashref );
66 sort_hash( $hashref, qw/ noempty nofatal alpha desc /);
67  
68 =head2 Errors
69  
70 Numeric sorts will fail if given a non-number. Normally alpha sorts will
71 treat numbers as text. strictalpha uses Scalar::Util::looks_like_number
72 to reject a hash that has any values that appear to be numbers.
73  
74 When the data is illegal for the sort type in effect, (only alpha has no restriction) sort_hash will die. If you prefer it not to, use nofatal to return () and warn instead of die, silent (implies nofatal) will just return () without a warning.
75  
76 Sorting an empty hashref will return nothing (). You can make this into an error that will die or warn depending on the nofatal flag with noempty.
77  
78 =head1 Changes from Version 1.x to 2.x
79  
80 The API has been changed from version 1. It is no longer possible to pass a naked hash, and it is no longer necessary to enter parameters as key value pairs. The default has also been changed from nofatal (warn only) to fatal (die on illegal sort).
81  
82 Upgrading to version 2. If you passed a naked hash just precede it with a backslash to pass it as a hashref. Add the parameter 'nofatal' to warn instead of die. Version 2 takes its arguments as an array and just ignores the extra arguments that would come in from a version 1 call. If you were already passing a hashref it will just work, except that illegal values are fatal without nofatal.
83  
84 =head2 If you need version1 compatibility
85  
86 Version 1 is included in the version 2 distribution, renamed as Sort::Hash1, just change your use statement to C<use Sort::Hash1;>.
87  
88 =cut
89  
90 15 ▶sub sort_hash {
Subroutine sort_hash: called CC=18 SCAR=28.9
Pod documented
91 15 my @sorted = ();
92 # my $H = shift;
93 15 my $H = {}; # $H must be a hashref, others are ints.
94 15 my ( $silent, $nofatal, $noempty, $desc, $alpha, $strictalpha ) = 0;
95 15 my ( $numeric, $asc ) = 1;
96 15 for (@_) {
97 32 ▶ if ( ref $_ eq 'HASH') { $H = $_ };
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq "HASH") 15 17 32
98 32 ▶ if ( $_ eq 'nofatal' ) { $nofatal = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "nofatal") 4 28 32
99 32 ▶ if ( $_ eq 'silent' ) { $silent = 1; $nofatal = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "silent") 2 30 32
100 32 ▶ if ( $_ eq 'noempty' ) { $noempty = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "noempty") 1 31 32
101 32 ▶ if ( $_ eq 'desc' ) { $desc = 1; $asc = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "desc") 1 31 32
102 0 ▶ if ( $_ eq 'asc' ) { $asc = 1; $desc = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "asc") 0 32 32
103 32 ▶ if ( $_ eq 'alpha' ) { $alpha = 1; $numeric = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "alpha") 4 28 32
104 32 ▶ if ( $_ eq 'strictalpha' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "strictalpha") 1 31 32
105 1 $strictalpha = 1;
106 1 $alpha = 1;
107 1 $numeric = 0;
108 }
109 32 ▶ if ( $_ eq 'numeric' ) { $strictalpha = 0; $alpha = 0; $numeric = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "numeric") 4 28 32
110 }
111  
112 6 ▶ my $death = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
113 6 ▶ if ($nofatal) { warn $_[0] unless $silent; return (); }
Branch: 2 branches
BranchTrueFalseTotal
if ($nofatal) { } 4 2 6
unless $silent 2 2 4
114 2 else { die $_[0]; }
115 15 };
116 # $H initialized at 0, but if a hash was provided
117 #if( $H == 0 ) { die 'No Hash was provided for sorting.'}
118 15 ▶ if ($noempty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($noempty) 1 14 15
119 1 ▶ unless ( scalar( keys %$H ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %$H) 1 0 1
120 1 $death->(
121 'Attempt to sort an empty hash while noempty is in effect');
122 }
123 }
124 14 ▶ if ($strictalpha) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strictalpha) 1 13 14
125 1 for ( values %{$H}) {
126 3 ▶ if ( Scalar::Util::looks_like_number($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Scalar::Util::looks_like_number($_)) 1 2 3
127 1 $death->(
128 'Attempt to Sort Numeric Value in Strict Alpha Sort');
129 1 return ;
130 }
131 }
132 }
133 13 ▶ if ($alpha) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alpha) { } 4 9 13
134 4 @sorted = ( sort { lc $H->{$a} cmp lc $H->{$b} } keys %{$H} );
135 }
136 else {
137 9 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
138 9 @sorted = ( sort { $H->{$a} <=> $H->{$b} } keys %{$H} );
139 }
140 4 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
141 4 $death->('Attempt to Sort non-Numeric values in a Numeric Sort');
142 3 return ;
143 }
144 9 }
145 12 ▶ if ( $desc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc) { } 1 11 12
146 1 return reverse @sorted;
147 }
148 11 else { return @sorted; }
149 }
150  
151 =pod
152  
153 =head1 AUTHOR
154  
155 John Karr, C<brainbuz at brainbuz.org>
156  
157 =head1 BUGS
158  
159 Please report any bugs or feature requests via the BitBucket issue tracker at
160 L<https://bitbucket.org/brainbuz/sort-hash/issues>. I will be
161 notified, and then you'll automatically be notified of progress on
162 your bug as I make changes.
163  
164 =head1 SUPPORT
165  
166 You can find documentation for this module with the perldoc command.
167  
168 You can also look for information at: The documentation for the
169 sort command in the Perl documentation.
170  
171 =head1 LICENSE AND COPYRIGHT
172  
173 Copyright 2014 John Karr.
174  
175 This program is free software; you can redistribute it and/or modify
176 it under the terms of the GNU General Public License as published by
177 the Free Software Foundation; version 3 or at your option
178 any later version.
179  
180 This program is distributed in the hope that it will be useful,
181 but WITHOUT ANY WARRANTY; without even the implied warranty of
182 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
183 GNU General Public License for more details.
184  
185 A copy of the GNU General Public License is available in the source tree;
186 if not, write to the Free Software Foundation, Inc.,
187 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
188  
189 =cut
190  
191 1 1;
↑ Summary Hash1.pm »