| 1 | package App::CSVUtils::csv_sort_fields_by_spec; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.010001; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |
| 8 | 1 | our $DATE = '2026-07-09'; # DATE | |
| 9 | 1 | our $DIST = 'App-CSVUtils'; # DIST | |
| 10 | 1 | our $VERSION = '1.038'; # VERSION | |
| 11 | |||
| 12 | 1 | ▶ | use App::CSVUtils; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use App::CSVUtils::csv_sort_fields; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use Perinci::Sub::Util qw(gen_modified_sub); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | |||
| 16 | my $res = gen_modified_sub( | ||
| 17 | output_name => 'csv_sort_fields_by_spec', | ||
| 18 | base_name => 'App::CSVUtils::csv_sort_fields::csv_sort_fields', | ||
| 19 | summary => 'Sort CSV fields by spec', | ||
| 20 | description => <<'MARKDOWN', | ||
| 21 | |||
| 22 | This is a thin wrapper for <prog:csv-sort-fields> to allow you to sort "by | ||
| 23 | spec". Sorting by spec is an advanced form of sorting by example. In addition to | ||
| 24 | specifying strings of examples, you can also specify regexes or Perl sorter | ||
| 25 | codes. For more details, see the sorting backend module <pm:Sort::BySpec>. | ||
| 26 | |||
| 27 | To specify a regex on the command-line, use one of these forms: | ||
| 28 | |||
| 29 | /.../ | ||
| 30 | qr(...) | ||
| 31 | |||
| 32 | and to specify Perl code on the command-line, use this form: | ||
| 33 | |||
| 34 | sub { ... } | ||
| 35 | |||
| 36 | For example, modifying from example in `Sort::BySpec`'s Synopsis, you want to | ||
| 37 | sort these fields: | ||
| 38 | |||
| 39 | field1..field15 field42 | ||
| 40 | |||
| 41 | as follow: 1) put fields with odd numbers first in ascending order; 2) put the | ||
| 42 | specific fields field4, field2, field42 in that order; 3) put the remaining | ||
| 43 | fields of even numbers in descending order. To do this: | ||
| 44 | |||
| 45 | % perl -E'say join(",",map {"field$_"} 1..15,42)' | csv-sort-fields-by-spec - \ | ||
| 46 | 'qr([13579]\z)' 'sub { my($a,$b)=@_; for($a,$b){s/field//} $a<=>$b }' \ | ||
| 47 | field4 field2 field42 \ | ||
| 48 | 'sub { my($a,$b)=@_; for($a,$b){s/field//} $b<=>$a }' | ||
| 49 | |||
| 50 | The result: | ||
| 51 | |||
| 52 | field1,field3,field5,field7,field9,field11,field13,field15,field4,field2,field42,field14,field12,field10,field8,field6 | ||
| 53 | |||
| 54 | MARKDOWN | ||
| 55 | remove_args => ['by_examples', 'by_code', 'by_sortsub'], | ||
| 56 | add_args => { | ||
| 57 | specs => { | ||
| 58 | 'x.name.is_plural' => 1, | ||
| 59 | 'x.name.singular' => 'spec', | ||
| 60 | summary => 'Spec entries', | ||
| 61 | 'summary.alt.plurality.singular' => 'Add a spec entry', | ||
| 62 | schema => ['array*', of=>'str_or_re_or_code*'], | ||
| 63 | req => 1, | ||
| 64 | pos => 1, | ||
| 65 | slurpy => 1, | ||
| 66 | completion => \&App::CSVUtils::_complete_field, | ||
| 67 | }, | ||
| 68 | }, | ||
| 69 | modify_args => { | ||
| 70 | 1 | ▶ | output_filename => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 71 | 1 | my $argspec = shift; | |
| 72 | 1 | delete $argspec->{pos}; | |
| 73 | }, | ||
| 74 | }, | ||
| 75 | tags => ['category:sorting'], | ||
| 76 | 0 | ▶ | output_code => sub { |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 77 | 0 | my %args = @_; | |
| 78 | 0 | my $spec = delete $args{specs}; | |
| 79 | 0 | require Sort::BySpec; | |
| 80 | 0 | App::CSVUtils::csv_sort_fields::csv_sort_fields( | |
| 81 | %args, | ||
| 82 | by_code => Sort::BySpec::cmp_by_spec(spec=>$spec), | ||
| 83 | ); | ||
| 84 | }, | ||
| 85 | 1 | ); | |
| 86 | |||
| 87 | 1 | 1; | |
| 88 | # ABSTRACT: Sort CSV fields by spec | ||
| 89 | |||
| 90 | __END__ | ||