File Coverage

blib/lib/Sort/Hash1.pm
Criterion Covered Total %
statement 41 41 100.0
branch 10 10 100.0
condition 8 8 100.0
subroutine 8 8 100.0
pod 1 1 100.0
total 68 68 100.0


line stmt bran cond sub pod time code
1             package Sort::Hash1;
2             {
3             $Sort::Hash1::VERSION = '2.05';
4             }
5 1     1   42553 use Exporter 'import';
  1         3  
  1         43  
6 1     1   15883 use Try::Tiny 0.13;
  1         1760  
  1         72  
7 1     1   8 use Scalar::Util 1.24;
  1         23  
  1         54  
8 1     1   6 use strict;
  1         1  
  1         32  
9 1     1   4 use warnings FATAL => 'all';
  1         3  
  1         377  
10              
11             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             sub sort_hash {
36 9     9 1 3263 my %H = @_;
37 9         17 my @sorted = ();
38 9   100     40 my $direction = delete $H{direction} || 'asc';
39 9   100     34 my $alpha = delete $H{alpha} || 0;
40 9   100     35 my $strictalpha = delete $H{strictalpha} || 0;
41 9   100     32 my $numeric = delete $H{numeric} || 1;
42 9 100       22 if ( defined $H{hashref} ) { %H = %{ $H{hashref} } }
  1         2  
  1         8  
43 9 100       19 if ($strictalpha) {
44 1         2 $alpha = 1;
45 1         4 for ( values %H ) {
46 2 100       10 if (Scalar::Util::looks_like_number($_)) {
47 1         9 warn 'Attempt to Sort Numeric Value in Strict Alpha Sort';
48 1         54 return undef }
49             }
50             }
51 8 100       15 if ($alpha) {
52 4         20 @sorted = ( sort { lc $H{$a} cmp lc $H{$b} } keys %H ) ;
  85         189  
53             }
54             else {
55 4     4   235 try { @sorted = ( sort { $H{$a} <=> $H{$b} } keys %H ) ;}
  71         108  
56             catch {
57 1     1   20 warn 'Attempt to Sort non-Numeric values in a Numeric Sort';
58 1         59 return undef ; }
59 4         33 }
60 8 100       72 if ( lc($direction) eq 'desc' ) {
61 1         8 return reverse @sorted;
62             }
63 7         676 else { return @sorted; }
64             }
65              
66             =head1 AUTHOR
67              
68             John Karr, C<< >>
69              
70             =head1 BUGS
71              
72             Please report any bugs or feature requests via the BitBucket issue tracker at
73             L. 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;