source: gs2-extensions/parallel-building/trunk/src/perllib/cpan/Sort/Key/Maker.pm@ 27037

Last change on this file since 27037 was 27037, checked in by jmt12, 11 years ago

Allow natural sorting in Perl

File size: 3.0 KB
Line 
1package Sort::Key::Maker;
2
3our $VERSION = '0.02';
4
5use warnings;
6use strict;
7
8use Sort::Key qw(multikeysorter multikeysorter_inplace);
9
10use Carp;
11our @CARP_NOT = qw(Sort::Key);
12
13sub 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
231;
24
25__END__
26
27=head1 NAME
28
29Sort::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
55Sort::Key::Maker is a pragmatic module that provides an easy to use
56interface to Sort::Key multikey sorting functionality.
57
58It creates multikey sorting functions on the fly for any key type
59combination and exports them to the caller package.
60
61The key types natively accepted are:
62
63 string, str, locale, loc, integer, int,
64 unsigned_integer, uint, number, num
65
66and support for other types can be added via L<Sort::Key::Register> (or
67also 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
75exports two subroutines to the caller package: C<foo_sort (&@)> and
76C<foo_sort_inplace (&\@)>.
77
78Those two subroutines require a sub reference as their first argument
79and then respectively, the list to be sorted or an array.
80
81For 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
92when the first argument after the sorter name is a reference to a
93subroutine it is used as the multikey extraction function. The
94generated 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
103L<Sort::Key>, L<Sort::Key::Register>.
104
105L<Sort::Maker> also available from CPAN provides similar
106functionality.
107
108=head1 AUTHOR
109
110Salvador FandiE<ntilde>o, E<lt>[email protected]<gt>
111
112=head1 COPYRIGHT AND LICENSE
113
114Copyright (C) 2005 by Salvador FandiE<ntilde>o
115
116This library is free software; you can redistribute it and/or modify
117it under the same terms as Perl itself, either Perl version 5.8.4 or,
118at your option, any later version of Perl 5 you may have available.
119
120=cut
Note: See TracBrowser for help on using the repository browser.