App-CSVUtils

 view release on metacpan or  search on metacpan

lib/App/CSVUtils/csv_sort_fields_by_spec.pm  view on Meta::CPAN

package App::CSVUtils::csv_sort_fields_by_spec;

use 5.010001;
use strict;
use warnings;

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2025-02-04'; # DATE
our $DIST = 'App-CSVUtils'; # DIST
our $VERSION = '1.036'; # VERSION

use App::CSVUtils;
use App::CSVUtils::csv_sort_fields;
use Perinci::Sub::Util qw(gen_modified_sub);

my $res = gen_modified_sub(
    output_name => 'csv_sort_fields_by_spec',
    base_name => 'App::CSVUtils::csv_sort_fields::csv_sort_fields',
    summary => 'Sort CSV fields by spec',
    description => <<'MARKDOWN',

This is a thin wrapper for <prog:csv-sort-fields> to allow you to sort "by
spec". Sorting by spec is an advanced form of sorting by example. In addition to
specifying strings of examples, you can also specify regexes or Perl sorter
codes. For more details, see the sorting backend module <pm:Sort::BySpec>.

To specify a regex on the command-line, use one of these forms:

    /.../
    qr(...)

and to specify Perl code on the command-line, use this form:

    sub { ... }

For example, modifying from example in `Sort::BySpec`'s Synopsis, you want to
sort these fields:

    field1..field15 field42

as follow: 1) put fields with odd numbers first in ascending order; 2) put the
specific fields field4, field2, field42 in that order; 3) put the remaining
fields of even numbers in descending order. To do this:

    % perl -E'say join(",",map {"field$_"} 1..15,42)' | csv-sort-fields-by-spec - \
        'qr([13579]\z)' 'sub { my($a,$b)=@_; for($a,$b){s/field//} $a<=>$b }' \
        field4 field2 field42 \
        'sub { my($a,$b)=@_; for($a,$b){s/field//} $b<=>$a }'

The result:

    field1,field3,field5,field7,field9,field11,field13,field15,field4,field2,field42,field14,field12,field10,field8,field6

MARKDOWN
    remove_args => ['by_examples', 'by_code', 'by_sortsub'],
    add_args => {
        specs => {
            'x.name.is_plural' => 1,
            'x.name.singular' => 'spec',
            summary => 'Spec entries',
            'summary.alt.plurality.singular' => 'Add a spec entry',
            schema => ['array*', of=>'str_or_re_or_code*'],
            req => 1,
            pos => 1,
            slurpy => 1,
            completion => \&App::CSVUtils::_complete_field,
        },
    },
    modify_args => {
        output_filename => sub {
            my $argspec = shift;
            delete $argspec->{pos};
        },
    },
    tags => ['category:sorting'],
    output_code => sub {
        my %args = @_;
        my $spec = delete $args{specs};
        require Sort::BySpec;
        App::CSVUtils::csv_sort_fields::csv_sort_fields(
            %args,
            by_code => Sort::BySpec::cmp_by_spec(spec=>$spec),
        );
    },
);

1;
# ABSTRACT: Sort CSV fields by spec

__END__

=pod

=encoding UTF-8

=head1 NAME

App::CSVUtils::csv_sort_fields_by_spec - Sort CSV fields by spec

=head1 VERSION

This document describes version 1.036 of App::CSVUtils::csv_sort_fields_by_spec (from Perl distribution App-CSVUtils), released on 2025-02-04.

=for Pod::Coverage ^(on|after|before)_.+$

=head1 FUNCTIONS


=head2 csv_sort_fields_by_spec

Usage:

 csv_sort_fields_by_spec(%args) -> [$status_code, $reason, $payload, \%result_meta]

Sort CSV fields by spec.

This is a thin wrapper for L<csv-sort-fields> to allow you to sort "by
spec". Sorting by spec is an advanced form of sorting by example. In addition to
specifying strings of examples, you can also specify regexes or Perl sorter
codes. For more details, see the sorting backend module L<Sort::BySpec>.

To specify a regex on the command-line, use one of these forms:

 /.../
 qr(...)

and to specify Perl code on the command-line, use this form:

 sub { ... }

For example, modifying from example in C<Sort::BySpec>'s Synopsis, you want to
sort these fields:

 field1..field15 field42

as follow: 1) put fields with odd numbers first in ascending order; 2) put the
specific fields field4, field2, field42 in that order; 3) put the remaining
fields of even numbers in descending order. To do this:

 % perl -E'say join(",",map {"field$_"} 1..15,42)' | csv-sort-fields-by-spec - \
     'qr([13579]\z)' 'sub { my($a,$b)=@_; for($a,$b){s/field//} $a<=>$b }' \
     field4 field2 field42 \
     'sub { my($a,$b)=@_; for($a,$b){s/field//} $b<=>$a }'

The result:

 field1,field3,field5,field7,field9,field11,field13,field15,field4,field2,field42,field14,field12,field10,field8,field6

This function is not exported.

Arguments ('*' denotes required arguments):

=over 4

=item * B<ci> => I<bool>

(No description)

=item * B<inplace> => I<true>

Output to the same file as input.

Normally, you output to a different file than input. If you try to output to the
same file (C<-o INPUT.csv -O>) you will clobber the input file; thus the utility
prevents you from doing it. However, with this C<--inplace> option, you can
output to the same file. Like perl's C<-i> option, this will first output to a
temporary file in the same directory as the input file then rename to the final
file at the end. You cannot specify output file (C<-o>) when using this option,
but you can specify backup extension with C<-b> option.

Some caveats:

=over

=item * if input file is a symbolic link, it will be replaced with a regular file;

=item * renaming (implemented using C<rename()>) can fail if input filename is too long;

=item * value specified in C<-b> is currently not checked for acceptable characters;

=item * things can also fail if permissions are restrictive;

=back

=item * B<inplace_backup_ext> => I<str> (default: "")

Extension to add for backup of input file.

In inplace mode (C<--inplace>), if this option is set to a non-empty string, will
rename the input file using this extension as a backup. The old existing backup
will be overwritten, if any.

=item * B<input_escape_char> => I<str>

Specify character to escape value in field in input CSV, will be passed to Text::CSV_XS.

Defaults to C<\\> (backslash). Overrides C<--input-tsv> option.

=item * B<input_filename> => I<filename> (default: "-")



( run in 0.453 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )