| 1 | package App::CSVUtils::csv_sorted_rows; | ||
| 2 | |||
| 3 | 2 | ▶ | use 5.010001; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use Log::ger; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |
| 9 | 2 | our $DATE = '2026-07-09'; # DATE | |
| 10 | 2 | our $DIST = 'App-CSVUtils'; # DIST | |
| 11 | 2 | our $VERSION = '1.038'; # VERSION | |
| 12 | |||
| 13 | 2 | ▶ | use App::CSVUtils qw( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | gen_csv_util | ||
| 15 | 2 | ); | |
| 16 | 2 | ▶ | use App::CSVUtils::csv_sort_rows; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | |||
| 18 | gen_csv_util( | ||
| 19 | name => 'csv_sorted_rows', | ||
| 20 | summary => 'Check that CSV rows are sorted', | ||
| 21 | description => <<'MARKDOWN', | ||
| 22 | |||
| 23 | This utility checks that rows in the CSV are sorted according to specified | ||
| 24 | sorting rule(s). Example `input.csv`: | ||
| 25 | |||
| 26 | name,age | ||
| 27 | Andy,20 | ||
| 28 | Dennis,15 | ||
| 29 | Ben,30 | ||
| 30 | Jerry,30 | ||
| 31 | |||
| 32 | Example check command: | ||
| 33 | |||
| 34 | % csv-sorted-rows input.csv --by-field name; # check if name is ascibetically sorted | ||
| 35 | ERROR 400: Rows are NOT sorted | ||
| 36 | |||
| 37 | Example `input2.csv`: | ||
| 38 | |||
| 39 | name,age | ||
| 40 | Andy,20 | ||
| 41 | Ben,30 | ||
| 42 | Dennis,15 | ||
| 43 | Jerry,30 | ||
| 44 | |||
| 45 | % csv-sorted-rows input2.csv --by-field name; # check if name is ascibetically sorted | ||
| 46 | Rows are sorted | ||
| 47 | |||
| 48 | % csv-sorted-rows input2.csv --by-field ~name; # check if name is ascibetically sorted in descending order | ||
| 49 | ERROR 400: Rows are NOT sorted | ||
| 50 | |||
| 51 | See <prog:csv-sort-rows> for details on sorting options. | ||
| 52 | |||
| 53 | MARKDOWN | ||
| 54 | |||
| 55 | writes_csv => 0, | ||
| 56 | |||
| 57 | add_args => { | ||
| 58 | # KEEP SYNC WITH csv_sort_rows | ||
| 59 | %App::CSVUtils::argspecopt_hash, | ||
| 60 | %App::CSVUtils::argspecs_sort_rows, | ||
| 61 | |||
| 62 | quiet => { | ||
| 63 | summary => 'If set to true, do not show messages', | ||
| 64 | description => <<'MARKDOWN', | ||
| 65 | |||
| 66 | Normally a message will be printed to stdout saying whether the rows are sorted | ||
| 67 | or not, i.e. one of: | ||
| 68 | |||
| 69 | Rows are sorted | ||
| 70 | Rows are NOT sorted | ||
| 71 | |||
| 72 | If this option is specified, then no message will be printed. Instead, you can | ||
| 73 | find out whether things are sorted via exit code (or status code in the | ||
| 74 | enveloped result, if you request JSON or call this utility as a Perl function). | ||
| 75 | |||
| 76 | MARKDOWN | ||
| 77 | schema => 'bool*', | ||
| 78 | cmdline_aliases => {q=>{}}, | ||
| 79 | }, | ||
| 80 | }, | ||
| 81 | |||
| 82 | tags => ['category:checking'], | ||
| 83 | |||
| 84 | on_input_header_row => \&App::CSVUtils::csv_sort_rows::on_input_header_row, | ||
| 85 | |||
| 86 | on_input_data_row => \&App::CSVUtils::csv_sort_rows::on_input_data_row, | ||
| 87 | |||
| 88 | 2 | ▶ | after_close_input_files => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 89 | 2 | local $main::_CSV_SORTED_ROWS = 1; | |
| 90 | 2 | App::CSVUtils::csv_sort_rows::after_close_input_files(@_); | |
| 91 | }, | ||
| 92 | 2 | ); | |
| 93 | |||
| 94 | 2 | 1; | |
| 95 | # ABSTRACT: Check that CSV rows are sorted | ||
| 96 | |||
| 97 | __END__ | ||