blib/lib/Sort/Hash/Values.pm

filter: Statementstmt 89.6% 26 / 29 Branchbran 50.0% 1 / 2 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 85.7% 6 / 7 Podpod 100% 1 / 1 totaltotal 83.3% 35 / 42 CC 3 SCAR 6.0
CC
3
Cov
85%
sort_values
3.1
__ANON__
2.0
BEGIN
1.0
CRAP 1.8
↑ Summary
1 package Sort::Hash::Values;
2 {
3 1 $Sort::Hash::Values::VERSION = '0.1.1';
4 }
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Exporter 5.59 qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 our @EXPORT = our @EXPORT_OK = qw/sort_values/;
9  
10 # Returns keys of sorted hash values
11 4 ▶sub sort_values(&@) {
Subroutine sort_values: called CC=3 SCAR=11.4
Pod documented
12 4 my ($code, %hash) = @_;
13  
14 # Perl has special behavior when code prototype is $$ of uploading
15 # sorted values to @_. || "" is needed to avoid undef warning.
16 4 ▶ if ((prototype $code || "") eq '$$') {
Branch: 1 branch
BranchTrueFalseTotal
if ((prototype $code || "") eq "\$\$") 0 4 4
Condition50%1/2
prototype $code || ''
l!l
04
MC/DC0%0/1
prototype $code || ''
prototype $code
Truth table50%1/2
A
A= prototype $code
Aresult
11
00
17 0 my $old_code = $code;
18 0 ▶ $code = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 0 $old_code->($a, $b);
20 }
21 0 }
22  
23 # I need caller, so the package to modify would be known.
24 4 my $pkg = caller;
25  
26 # I need this sub {} because for some reason I'm not allowed to use
27 # lexical pragmas in sort block.
28 18 ▶ my $by_values = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
29 # As I'm doing direct symbol table modifications, disable
30 # strict 'refs' for this subroutine.
31 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 # Localize $a and $b in caller package
33 18 local *{"${pkg}::a"} = \$a->[1];
34 18 local *{"${pkg}::b"} = \$b->[1];
35 18 $code->();
36 4 };
37 4 map $_->[0], sort $by_values map [$_ => $hash{$_}], keys %hash;
38 }
39  
40 # Positive value at end
41 1 1;
42  
43 =head1 NAME
44  
45 Sort::Hash::Values - sort hashes by values
46  
47 =head1 SYNOPSIS
48  
49 use Sort::Hash::Values;
50  
51 my %birth_dates = (
52 Larry => 1954,
53 Randal => 1961,
54 Damian => 1964,
55 Simon => 1978,
56 Mark => 1965,
57 Jesse => 1976,
58 );
59  
60 for my $name (sort_values { $a <=> $b } %birth_dates) {
61 printf "%7s was born in %s.\n", $name, $birth_dates{$name};
62 }
63  
64 =head1 DESCRIPTION
65  
66 C<sort_values()> is a function that returns keys of values after
67 sorting its values.
68  
69 =head1 EXPORTS
70  
71 All functions are exported using L<Exporter>. If you don't want this
72 (but why you would use this module then) try importing it using empty
73 list of functions.
74  
75 use Sort::Hash::Values ();
76  
77 =over 4
78  
79 =item sort_values { code } %hash
80  
81 The only function in this module. It sorts every value in hash using
82 specified code and returns list of their keys sorted according to their
83 values.
84  
85 Just like with C<sort> in Perl, when code prototype is C<$$>, the
86 variables to sort will be in C<@_> too.
87  
88 =back
89  
90 =head1 CAVEATS
91  
92 When giving the function to C<sort_values> it has to be function in
93 scope where you call C<sort_values>. This is internal limitation caused
94 by the fact that Perl module cannot know what package the function you
95 used belongs. It can only know where C<sort_values> was called. This
96 bug also affects C<reduce> in L<List::Util>, C<pairwise> in
97 L<List::MoreUtils> and other functions that use C<$a> or C<$b>
98 variables - those variables have to be modified in function's package.
99  
100 The code block isn't optional. I really would like to make it optional,
101 but I cannot with Perl limitations. Instead, use C<{ $a cmp $b }> just
102 after C<sort_values>.
103  
104 If you will make C<$a> or C<$b> lexical, except this module to break,
105 as they aren't referencing global variables anymore. This affects every
106 function that uses those variables, even C<sort> builtin.
107 L<Just don't|http://perl-begin.org/tutorials/bad-elements/#vars-a-and-b>.
108  
109 When using C<$a> or C<$b> only once in the code (with the exception for
110 C<sort> builtin), Perl will warn you. This also affects other modules
111 that use those variables. To remove warnings about this, use following
112 code.
113  
114 no warnings 'once';
115  
116 =head1 AUTHOR
117  
118 Konrad Borowski <glitchmr@myopera.com>
119  
120 =head1 COPYRIGHT AND LICENSE
121  
122 This software is copyright (c) 2012 by Konrad Borowski.
123  
124 This is free software; you can redistribute it and/or modify it under
125 the same terms as the Perl 5 programming language system itself.
↑ Summary