App-PTP
view release on metacpan or search on metacpan
lib/App/PTP/Args.pm view on Meta::CPAN
$m{output_field} = $default_output_field;
return %m;
}
sub get_default_options {
my %o;
$o{input_encoding} = 'UTF-8';
$o{output_encoding} = 'UTF-8';
$o{input_separator} = '\n'; # This will be interpreted in a regex
$o{output_separator} = "\n";
$o{preserve_eol} = 0;
$o{fix_final_separator} = 0;
$o{recursive} = 0;
$o{input_filter} = undef;
$o{debug_mode} = 0;
$o{merge} = 0;
$o{in_place} = 0;
$o{output} = undef;
$o{append} = 0;
$o{abort} = 0;
$o{preserve_perl_env} = 0;
lib/App/PTP/Args.pm view on Meta::CPAN
'input-separator|in-separator=s' => \$options{input_separator},
'output-separator|out-separator=s' => \$options{output_separator},
'fix-final-separator!' => \$options{fix_final_separator},
# TODO: add more tests for this mode, itâs unclear that the input separator
# is really used (also work if itâs set to something else).
'0' => sub {
$options{input_separator} = "\N{NULL}";
$options{output_separator} = '';
},
'00' => sub { $options{output_separator} = "\N{NULL}" },
'preserve-input-separator|eol' =>
sub { $options{preserve_eol} = 1; $options{output_separator} = '' },
'preserve-perl-env!' => \$options{preserve_perl_env},
'safe:2' => sub { $options{use_safe} = $_[1] },)
}
sub modes_flags {
(
'case-sensitive|S' => sub { $modes{case_sensitive} = 1 },
'case-insensitive|I' => sub { $modes{case_sensitive} = 0 },
'quote-regexp|Q' => sub { $modes{quote_regex} = 1 },
'end-quote-regexp|E' => sub { $modes{quote_regex} = 0 },
lib/App/PTP/Cheat_Sheet.pod view on Meta::CPAN
=item B<-R>, B<--recursive>, B<--input-filter> I<code>: expand directories,
optionally filter input files
=item B<--input-encoding> I<encoding>, B<--output-encoding> I<encoding>: default
is UTF-8
=item B<--input-separator> I<separator>, B<--output-separator> I<separator>:
default is C<\n>
=item B<--eol> (B<--preserve-input-separator>), B<--fix-final-separator>
=item B<-0>: set B<--input-separator> to C<NUL> and B<--output-separator> to the
empty string
=item B<--00>: set B<--output-separator> to C<NUL>, useful with C<xargs -0>
=item B<-h> (B<--help>), B<--cheat>, B<--helpshort>, B<--version>: remember to
have B<perldoc> installed
=item B<-d> (B<--debug>), B<--abort>
lib/App/PTP/Files.pm view on Meta::CPAN
if (not defined $content) {
if ($@) {
chomp($@);
die "FATAL: Cannot read input: $@\n";
}
# Theoretically this should not happen. But, on 5.22 this seems to happens
# if the input file is empty.
$content = '';
}
my @content;
if ($options->{preserve_eol}) {
@content = $content =~ /\G ( .*? (?n: $options->{input_separator} ))/xgcms;
} else {
@content = $content =~ /\G (.*?) (?n: $options->{input_separator} )/xgcms;
}
my $missing_final_separator = 0;
if ((pos($content) // 0) < length($content)) {
$missing_final_separator = 1;
print "The last line has no separator.\n" if $options->{debug_mode};
push @content, substr($content, pos($content) // 0);
}
Specify the separator that is used to split the lines in the input files. The
default is "\n" (LF). Note that currently, on windows, "\r\n" (CRLF) characters
in input files will be automatically transformed into "\n" characters.
=item B<--output-separator> I<separator> (alias B<--out-separator>)
Specify the separator that is added in-between lines in the output. The
default is "\n" (LF). Note that currently, on Windows, this is automatically
transformed into an "\r\n" (CRLF) sequence.
=item B<--eol>, B<--preserve-input-separator>
Keep the input separators in the content of each line. It is then your
responsibility to preserve (or to change) this separator when the files are
processed.
Setting this flag also sets the B<--output-separator> to the empty string. This
can be overridden if needed by passing that flag after the B<--eol> one
(this would result in each line having their initial end of line separator plus
the one specified for the output (unless the initial one is removed during the
processing).
=item B<--fix-final-separator>
If set, then the final line of each file is always terminated by a line
separator in the output (as specified by B<--output-separator>), even if it
did not have one in the input.
Print a sorted list of the login name of all users:
ptp /etc/passwd -F : --cut 1 --sort
Number all the lines of multiple inputs, as if they were a single file:
ptp file1 file2 -m --nl
Join lines that end with an B<=> character with the next line. The B<chomp> Perl
command removes the end-of-line character from the current line (which was there
due to the usage of the B<--eol> flag). In this example, that command is applied
only if the line matches the given regex (which search for the B<=> character
at the end of the line):
ptp file --eol -p 'chomp if /=$/'
Output the number of lines of comment in all the source files in a given
directory, filtering only the files that match some extensions. The
B<--input-filter> option ensures that only source file are used inside the given
directory. The B<-g> (B<--grep>) command keeps only the lines that start with a
C-style comment (or spaces followed by a comment), then the B<--lc> command
(B<--line-count>), replaces the entire content of the file with just the number
of lines that it contains (the number of comments at that point). Finally, the
B<--pfn> command (B<--prefix-file-name>) adds the name of the current file as
the first line of each file, and B<--pivot> joins the two lines of each file
t/999-examples.t view on Meta::CPAN
}{
my $data = ptp(['-p', 'pf("%5d %s", ++$i, $_) if $_'],
'default_small.txt');
is($data, " 1 foobar\n 2 foobaz\n\n 3 last\n",
'count non empty');
}{
my $input = "User1:*:1\nOther:*:2\nLast User:password:3\n";
my $data = ptp([qw(-F : --cut 1 --sort)], \$input);
is($data, "Last User\nOther\nUser1\n", 'sorted user list');
}{
my $data = ptp(['--eol', '-p', 'chomp if /=$/'], 'default_data.txt');
is($data, "test\nfoobar=ab/cd\nBe\nfoobaz\n.\\+\n\nlast=last\n",
'joined lines');
}{
my $data =
ptp([qw(-R . --input-filter /\.(c|h|cc)$/ -g ^\s*// --lc --pfn --pivot)]);
is($data, "./src/fake.cc\t6\n./src/fake.h\t4\n", 'comment lines');
}
( run in 0.842 second using v1.01-cache-2.11-cpan-98e64b0badf )