File Coverage

blib/lib/Sort/Key/Multi.pm
Criterion Covered Total %
statement 32 33 96.9
branch 5 8 62.5
condition 1 2 50.0
subroutine 7 7 100.0
pod n/a
total 45 50 90.0


line stmt bran cond sub pod time code
1             package Sort::Key::Multi;
2              
3             our $VERSION = '1.30';
4              
5 1     1   616 use warnings;
  1         2  
  1         27  
6 1     1   4 use strict;
  1         1  
  1         35  
7              
8 1     1   5 use Sort::Key qw(multikeysorter multikeysorter_inplace);
  1         2  
  1         44  
9 1     1   5 use Sort::Key::Types;
  1         2  
  1         33  
10              
11 1     1   5 use Carp;
  1         1  
  1         327  
12             our @CARP_NOT = qw(Sort::Key);
13              
14             my %sub;
15             my %type = qw( i integer
16             u unsigned_integer
17             n number
18             s string
19             l locale);
20              
21             my $one_char_types = join('', keys %Sort::Key::Types::mktypes);
22              
23             sub import {
24 2     2   12 shift;
25 2         4 for my $name (@_) {
26 2         3 my $sub = $sub{$name};
27 2 50       4 unless (defined $sub) {
28 2 50       42 my ($types, $inplace) = $name =~ /^((?:r?[$one_char_types]\d*_*)+)keysort((?:_?inplace)?)$/o
29             or croak "invalid name for multikey sorter '$name'";
30 2         4 my @types;
31 2         11 while ($types =~ /(r?)(.)(\d*)_*/g) {
32 3         7 my ($r, $t, $n) = ($1, $2, $3);
33 3 100 50     23 push @types, ( ($r ? '-' : '') . $type{$t} ) x ($n || 1);
34             }
35             # print STDERR "$types => @types\n";
36 2 50       3 if ($inplace) {
37 0         0 $sub = multikeysorter_inplace(@types);
38             }
39             else {
40 2         5 $sub = multikeysorter(@types);
41             }
42             }
43 2         3 my $caller = caller;
44 1     1   5 no strict 'refs';
  1         6  
  1         78  
45 2         2 *{$caller."::".$name} = $sub;
  2         2492  
46             }
47             }
48              
49             1;
50              
51             =head1 NAME
52              
53             Sort::Key::Multi - simple multi-key sorts
54              
55             =head1 SYNOPSIS
56              
57             use Sort::Key::Multi qw(sikeysort);
58             my @data = qw(foo0 foo1 bar34 bar0 bar34 bar33 doz4)
59             my @sisorted = sikeysort { /(\w+)(\d+)/} @data;
60              
61             =head1 DESCRIPTION
62              
63             Sort::Key::Multi creates multi-key sorting subroutines and exports them
64             to the caller package.
65              
66             The names of the sorters are of the form C or
67             C, where C determines the number and types of
68             the keys as follows:
69              
70             =over 4
71              
72             + C indicates an integer key, C indicates an unsigned integer
73             key, C indicates a numeric key, C indicates a string key and
74             C indicates a string key that obeys locale order configuration.
75              
76             + Type characters can be prefixed by C to indicate reverse order.
77              
78             + A number following a type character indicates that the key type has
79             to be repeated as many times (for instance C is equivalent to
80             C and C is equivalent to C).
81              
82             + Underscores (C<_>) can be freely used between type indicators.
83              
84             =back
85              
86             For instance:
87              
88             use Key::Sort::Multi qw(iirskeysort
89             i2rskeysort
90             i_i_rs__keysort
91             i2rs_keysort);
92              
93             exports to the caller package fourth identical sorting functions that
94             take two integer keys that are sorted in ascending order and one
95             string key that is sorted in descending order.
96              
97             The generated sorters take as first argument a subroutine that is used
98             to extract the keys from the values which are passed inside C<$_>, for
99             example:
100              
101             my @data = qw(1.3.foo 1.3.bar 2.3.bar 1.4.bar 1.7.foo);
102             my @s = i2rs_keysort { split /\./, $_ } @data;
103              
104             =head1 SEE ALSO
105              
106             For a more general multi-key sorter generator see L.
107              
108             =head1 COPYRIGHT AND LICENSE
109              
110             Copyright (C) 2006, 2014 by Salvador FandiEo
111             Esfandino@yahoo.comE
112              
113             This library is free software; you can redistribute it and/or modify
114             it under the same terms as Perl itself, either Perl version 5.8.4 or,
115             at your option, any later version of Perl 5 you may have available.
116              
117             =cut