1 | package Sort::Key::Maker;
|
---|
2 |
|
---|
3 | our $VERSION = '0.02';
|
---|
4 |
|
---|
5 | use warnings;
|
---|
6 | use strict;
|
---|
7 |
|
---|
8 | use Sort::Key qw(multikeysorter multikeysorter_inplace);
|
---|
9 |
|
---|
10 | use Carp;
|
---|
11 | our @CARP_NOT = qw(Sort::Key);
|
---|
12 |
|
---|
13 | sub import {
|
---|
14 | my $class = shift;
|
---|
15 | my $name = shift;
|
---|
16 | my $caller = caller;
|
---|
17 |
|
---|
18 | no strict 'refs';
|
---|
19 | *{"${caller}::${name}"} = multikeysorter @_;
|
---|
20 | *{"${caller}::${name}_inplace"} = multikeysorter_inplace @_;
|
---|
21 | }
|
---|
22 |
|
---|
23 | 1;
|
---|
24 |
|
---|
25 | __END__
|
---|
26 |
|
---|
27 | =head1 NAME
|
---|
28 |
|
---|
29 | Sort::Key::Maker - multikey sorter creator
|
---|
30 |
|
---|
31 | =head1 SYNOPSYS
|
---|
32 |
|
---|
33 | # create a function that sorts strings by length:
|
---|
34 | use Sort::Key::Maker sort_by_length => sub { length $_}, qw(integer);
|
---|
35 |
|
---|
36 | # create a multikey sort function;
|
---|
37 | # first key is integer sorted in descending order,
|
---|
38 | # second key is a string in default (ascending) order:
|
---|
39 | use Sort::Key::Maker ri_s_keysort => qw(-integer string);
|
---|
40 |
|
---|
41 | # some sample data...
|
---|
42 | my @foo = qw(foo bar t too tood mama);
|
---|
43 |
|
---|
44 | # and now, use the sorter functions previously made:
|
---|
45 |
|
---|
46 | # get the values on @foo sorted by length:
|
---|
47 | my @sorted = sort_by_length @foo;
|
---|
48 |
|
---|
49 | # sort @foo inplace by its length and then by its value:
|
---|
50 | ri_s_keysort_inplace { length $_, $_ } @foo;
|
---|
51 |
|
---|
52 |
|
---|
53 | =head1 DESCRIPTION
|
---|
54 |
|
---|
55 | Sort::Key::Maker is a pragmatic module that provides an easy to use
|
---|
56 | interface to Sort::Key multikey sorting functionality.
|
---|
57 |
|
---|
58 | It creates multikey sorting functions on the fly for any key type
|
---|
59 | combination and exports them to the caller package.
|
---|
60 |
|
---|
61 | The key types natively accepted are:
|
---|
62 |
|
---|
63 | string, str, locale, loc, integer, int,
|
---|
64 | unsigned_integer, uint, number, num
|
---|
65 |
|
---|
66 | and support for other types can be added via L<Sort::Key::Register> (or
|
---|
67 | also via L<Sort::Key::register_type()>).
|
---|
68 |
|
---|
69 | =head2 USAGE
|
---|
70 |
|
---|
71 | =over 4
|
---|
72 |
|
---|
73 | =item use Sort::Key::Maker foo_sort =E<gt> @keys;
|
---|
74 |
|
---|
75 | exports two subroutines to the caller package: C<foo_sort (&@)> and
|
---|
76 | C<foo_sort_inplace (&\@)>.
|
---|
77 |
|
---|
78 | Those two subroutines require a sub reference as their first argument
|
---|
79 | and then respectively, the list to be sorted or an array.
|
---|
80 |
|
---|
81 | For instance:
|
---|
82 |
|
---|
83 | use Sort::Key::Maker bar_sort => qw(int int str);
|
---|
84 |
|
---|
85 | @bar=qw(doo tomo 45s tio);
|
---|
86 | @sorted = bar_sort { unpack "CCs", $_ } @bar;
|
---|
87 | # or sorting @bar inplace
|
---|
88 | bar_sort_inplace { unpack "CCs", $_ } @bar;
|
---|
89 |
|
---|
90 | =item use Sort::Key::Maker foo_sort =E<gt> \&genmultikey, @keys;
|
---|
91 |
|
---|
92 | when the first argument after the sorter name is a reference to a
|
---|
93 | subroutine it is used as the multikey extraction function. The
|
---|
94 | generated sorter functions doesn't require neither accept one, i.e.:
|
---|
95 |
|
---|
96 | use Sort::Key::Maker sort_by_length => sub { length $_ }, 'int';
|
---|
97 | my @sorted = sort_by_length qw(foo goo h mama picasso);
|
---|
98 |
|
---|
99 | =back
|
---|
100 |
|
---|
101 | =head1 SEE ALSO
|
---|
102 |
|
---|
103 | L<Sort::Key>, L<Sort::Key::Register>.
|
---|
104 |
|
---|
105 | L<Sort::Maker> also available from CPAN provides similar
|
---|
106 | functionality.
|
---|
107 |
|
---|
108 | =head1 AUTHOR
|
---|
109 |
|
---|
110 | Salvador FandiE<ntilde>o, E<lt>[email protected]<gt>
|
---|
111 |
|
---|
112 | =head1 COPYRIGHT AND LICENSE
|
---|
113 |
|
---|
114 | Copyright (C) 2005 by Salvador FandiE<ntilde>o
|
---|
115 |
|
---|
116 | This library is free software; you can redistribute it and/or modify
|
---|
117 | it under the same terms as Perl itself, either Perl version 5.8.4 or,
|
---|
118 | at your option, any later version of Perl 5 you may have available.
|
---|
119 |
|
---|
120 | =cut
|
---|