| 1 | # This module contains the function that are pushed in the pipelines and which | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # are operating on the input files. | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | package App::PTP::Commands; | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use 5.022; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use App::PTP::Files qw(write_side_output read_side_input write_handle); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use App::PTP::PerlEnv; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use App::PTP::Util; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use App::PTP::Util::Semver; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Cwd qw(abs_path); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use File::Spec::Functions qw(rel2abs); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use List::Util qw(min max); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use Safe; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 2 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | # Every public function is exported by default. | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | my @all_cmd = qw(prepare_perl_env do_grep do_substitute do_perl | |||||||||||||||||||||||||||||||||||||||||
| 26 | do_execute do_load do_sort do_list_op do_tail do_head do_delete_marked | ||||||||||||||||||||||||||||||||||||||||||
| 27 | do_insert_marked do_set_markers do_number_lines do_file_name do_line_count | ||||||||||||||||||||||||||||||||||||||||||
| 28 | do_cut do_paste do_pivot do_tee do_shell do_eat do_push); | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 2 | our @EXPORT_OK = (@all_cmd, 'warn_or_die_if_needed'); | |||||||||||||||||||||||||||||||||||||||||
| 30 | 2 | our %EXPORT_TAGS = (CMD => \@all_cmd); | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 2 | our @markers; # The current marker array, not shared in the safe. | |||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | tie @App::PTP::PerlEnv::m, 'App::PTP::Util::MarkersArray'; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 2 | my @content_stack; # The push/pop stack, not shared in the safe. | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | my $f_setter = tie $App::PTP::PerlEnv::f, 'App::PTP::Util::ReadOnlyVar'; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | my $F_setter = tie $App::PTP::PerlEnv::F, 'App::PTP::Util::ReadOnlyVar'; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | my $n_setter = tie $App::PTP::PerlEnv::n, 'App::PTP::Util::ReadOnlyVar'; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | my $N_setter = tie $App::PTP::PerlEnv::N, 'App::PTP::Util::ReadOnlyVar'; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | my $m_setter = tie $App::PTP::PerlEnv::m, 'App::PTP::Util::AliasVar'; | |||||||||||||||||||||||||||||||||||||||||
| 41 | # This variable is public because it is set in the App::PTP::process_all method. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | our $I_setter = tie $App::PTP::PerlEnv::I, 'App::PTP::Util::ReadOnlyVar'; | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | # This 'safe' provides an isolated environment for all the regex and perl code | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # execution provided by the user. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 2 | my $safe; # Sets to Safe->new() before each new file processed; | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 2 | my %modules_to_load = ( | |||||||||||||||||||||||||||||||||||||||||
| 49 | 'Data::Dumper' => [], | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 'File::Basename' => [], | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 'File::Copy' => [], | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 'File::Path' => [qw(make_path remove_tree mkpath rmtree)], | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 'File::Spec::Functions' => [':ALL'], | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 'List::Util' => [@List::Util::EXPORT_OK], | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 'Math::Trig' => [], | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 'Text::Tabs' => [], | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 'Text::Wrap' => [qw(wrap fill $columns $break $huge)], | ||||||||||||||||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub list_to_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_to_string: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | return join(', ', map { "'$_'" } @_) if @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | return ''; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | # Prepare the $safe variable so that it can execute code with access to our | ||||||||||||||||||||||||||||||||||||||||||
| 66 | # PerlEnv. | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub new_safe { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_safe: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | my ($options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ( $safe | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$safe and $options->{"preserve_perl_env"} and $safe->reval("\$PerlEnv_LOADED")
$safe and $options->{"preserve_perl_env"}
MC/DC0%0/3
$safe and $options->{"preserve_perl_env"} and $safe->reval("\$PerlEnv_LOADED")
$safe
$options->{"preserve_perl_env"}
$safe->reval("\$PerlEnv_LOADED")
Truth table0%0/4
A and B and C
A= $safeB= $options->{"preserve_perl_env"}C= $safe->reval("\$PerlEnv_LOADED")
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | and $options->{preserve_perl_env} | ||||||||||||||||||||||||||||||||||||||||||
| 71 | and $safe->reval('$PerlEnv_LOADED')) { | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | print "Skipping creation of new safe.\n" if $options->{debug_mode}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | print "Creating a new safe.\n" if $options->{debug_mode}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $safe = Safe->new(); | |||||||||||||||||||||||||||||||||||||||||
| 77 | # Loading arbitrary modules in the Safe does not work well so we don’t do it | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # here. | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | $safe->share_from('App::PTP::PerlEnv', \@App::PTP::PerlEnv::EXPORT_OK); | |||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ($options->{use_safe} > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $safe->permit_only( | |||||||||||||||||||||||||||||||||||||||||
| 82 | qw(:base_core :base_mem :base_loop :base_math :base_orig | ||||||||||||||||||||||||||||||||||||||||||
| 83 | :load)); | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | $safe->deny(qw(tie untie bless)); | |||||||||||||||||||||||||||||||||||||||||
| 85 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $safe->deny_only(qw(:subprocess :ownprocess :others :dangerous)); | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | chomp($@); | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | die "INTERNAL ERROR: cannot load the PerlEnv module: ${@}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | # Prepare an App::PTP::SafeEnv package with access to the PerlEnv one and | ||||||||||||||||||||||||||||||||||||||||||
| 95 | # nothing else. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub reset_safe_env { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reset_safe_env: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my ($options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if ($options->{preserve_perl_env} and $App::PTP::SafeEnv::PerlEnv_LOADED) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$options->{"preserve_perl_env"} and $App::PTP::SafeEnv::PerlEnv_LOADED
MC/DC0%0/2
$options->{"preserve_perl_env"} and $App::PTP::SafeEnv::PerlEnv_LOADED
$options->{"preserve_perl_env"}
$App::PTP::SafeEnv::PerlEnv_LOADED
Truth table0%0/3
A and B
A= $options->{"preserve_perl_env"}B= $App::PTP::SafeEnv::PerlEnv_LOADED
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | print "Skipping reset of perl environment.\n" if $options->{debug_mode}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | print "Resetting the perl environment.\n" if $options->{debug_mode}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | # We can't undef the hash, otherwise the code compiled in the eval below no | ||||||||||||||||||||||||||||||||||||||||||
| 104 | # longer refers to the same package (this would work however with a | ||||||||||||||||||||||||||||||||||||||||||
| 105 | # string-eval instead of a block-eval as the code would be compiled and | ||||||||||||||||||||||||||||||||||||||||||
| 106 | # create a new package hash). | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | %App::PTP::SafeEnv:: = (); | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | !eval { | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | package App::PTP::SafeEnv; | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | App::PTP::PerlEnv->import(':all'); | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | our $PerlEnv_LOADED = 1; # For some reason the import does not work | |||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | chomp($@); | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | die "INTERNAL ERROR: cannot prepare the SafeEnv package: ${@}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | while (my ($m, $i) = each %modules_to_load) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | eval("package App::PTP::SafeEnv; use $m ".list_to_string(@{$i})); ## no critic (Eval) | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | # Delete the PerlEnv (both the safe and the eval based one). This method is only | ||||||||||||||||||||||||||||||||||||||||||
| 126 | # meant to be called from tests. | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub delete_perl_env { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_perl_env: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | undef $safe; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | %App::PTP::SafeEnv:: = (); | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | # Should be called for each new file so that the environment seen by the user | ||||||||||||||||||||||||||||||||||||||||||
| 133 | # supplied Perl code is empty. | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub prepare_perl_env { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_perl_env: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | my ($file_name, $options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | if (ref($file_name)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | $f_setter->set('-'); | |||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | $F_setter->set('-'); | |||||||||||||||||||||||||||||||||||||||||
| 139 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | $f_setter->set($file_name); | |||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | $F_setter->set(abs_path($file_name)); | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if ($options->{use_safe} > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | new_safe($options); | |||||||||||||||||||||||||||||||||||||||||
| 145 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | reset_safe_env($options); | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | # process($file_name, \@pipeline, \%options, \@content, $missing_final_sep) | ||||||||||||||||||||||||||||||||||||||||||
| 151 | # Applies all the stage of the pipeline on the given content (which is modified | ||||||||||||||||||||||||||||||||||||||||||
| 152 | # in place). | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub process { ## no critic (RequireArgUnpacking) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | # We are unpacking the arguments but later we are taking a reference on $_[0] | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | my ($file_name, $pipeline, $options, $content, $missing_final_separator) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | if ($options->{debug_mode}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | # For long files, we print only the first and last lines. | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | my @debug_content = @$content; | |||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | Readonly::Scalar my $NB_DEBUG_LINES => 4; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | Readonly::Scalar my $MAX_LINES_TO_PRINT => 10; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | my $omit_msg = sprintf '... (%d lines omitted)', (@$content - ($NB_DEBUG_LINES * 2)); | |||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | splice @debug_content, $NB_DEBUG_LINES, -$NB_DEBUG_LINES, $omit_msg | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | if @$content > $MAX_LINES_TO_PRINT; | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | if (ref($file_name)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | print "Processing $${file_name} with content: ".Dumper(\@debug_content); | |||||||||||||||||||||||||||||||||||||||||
| 166 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | print "Processing '${file_name}' with content: ".Dumper(\@debug_content); | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | print 'Has final separator: '.($missing_final_separator ? 'false' : 'true')."\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | prepare_perl_env($file_name, $options); | |||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | @markers = (0) x scalar(@$content); | |||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | my $markers = \@markers; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | @content_stack = (); | |||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | for my $stage (@$pipeline) { | |||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | my ($command, $code, $modes, @args) = @$stage; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | $N_setter->set(scalar(@$content)); | |||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | $modes->{missing_final_separator} = $missing_final_separator; | |||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | $modes->{file_name_ref} = \$_[0]; # this is an alias to the passed value. | |||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | if ($options->{debug_mode}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | local $Data::Dumper::Indent = 0; | |||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | printf "Executing command: %s(%s).\n", $command, join(', ', map { Dumper($_) } @args); | |||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | &$code($content, $markers, $modes, $options, @args); | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | sub base_prepare_re { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
base_prepare_re: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | my ($re, $modes) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | if ($modes->{quote_regex}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | $re = quotemeta($re); | |||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | if (not $modes->{case_sensitive}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | $re = '(?i)'.$re; | |||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | return $re; | |||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | # prepare_re('re', \%options) | ||||||||||||||||||||||||||||||||||||||||||
| 200 | # Applies the modal option on the given regex. | ||||||||||||||||||||||||||||||||||||||||||
| 201 | # This function is not exported. | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | ▶ | sub prepare_re { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_re: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | my ($re, $modes) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | $re = base_prepare_re($re, $modes); | |||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | my $r; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | if ($modes->{regex_engine} ne 'perl') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | # Some play to correctly escape whatever special characters might be in the | ||||||||||||||||||||||||||||||||||||||||||
| 208 | # regex while preserving its semantics. This relies on the fact that the | ||||||||||||||||||||||||||||||||||||||||||
| 209 | # 'Terse' option of Data::Dumper is set in the main program. | ||||||||||||||||||||||||||||||||||||||||||
| 210 | # The regex-engine variable has been validated in the Args module. | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | my $str_re = Dumper($re); | |||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | my $engine = 're::engine::'.$modes->{regex_engine}; | |||||||||||||||||||||||||||||||||||||||||
| 213 | # We could the safe to avoid this perlcritic warning but we are not actually | ||||||||||||||||||||||||||||||||||||||||||
| 214 | # executing code here, so the safe is not needed (and we would need to find | ||||||||||||||||||||||||||||||||||||||||||
| 215 | # variable names that are guaranteed not to collide with user code). | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | $r = eval "use ${engine}; \$re = $str_re; qr/\$re/s"; ## no critic (ProhibitStringyEval) | |||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | chomp($@); | |||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | die "FATAL: Cannot use the specified regex engine: ${@}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | $r = qr/$re/s; | |||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | return $r; | |||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | sub quote_for_re { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
quote_for_re: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | my ($text, $modes) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | ▶ | if ($modes->{quote_regex}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | return quotemeta($text); | |||||||||||||||||||||||||||||||||||||||||
| 231 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 232 | # We quote just the '{' or '}' characters. | ||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | return $text =~ s/(\{|\})/\\$1/gr; | |||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | ▶ | sub prepare_re2 { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_re2: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | my ($re, $modes) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | $re = quote_for_re($re, $modes); | |||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | ▶ | if (not $modes->{case_sensitive}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | $re = '(?i)'.$re; | |||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | my $use_statement = ''; | |||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | ▶ | if ($modes->{regex_engine} ne 'perl') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | $use_statement = "use re::engine::$modes->{regex_engine};"; | |||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | return ($use_statement, "{${re}}"); | |||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | # This interpolate str in the Perl env (unless -Q is in effect). | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | ▶ | sub maybe_interpolate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
maybe_interpolate: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | my ($str, $modes, $options, $command) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | ▶ | if (not $modes->{quote_regex}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | $str = eval_in_safe_env("<<\"PTP_EOF_WORD\"\n${str}\nPTP_EOF_WORD\n", $options); | |||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | ▶ | die "FATAL: Cannot eval string for --${command}: ${@}\n" if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | chomp($str); | |||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | return $str; | |||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | sub do_grep { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_grep: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | my ($content, $markers, $modes, $options, $re) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | my ($use_stmt, $quoted_re) = prepare_re2($re, $modes); | |||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | ▶ | print "\$re = ${quoted_re}\n" if $options->{debug_mode}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | my $conjunction = $modes->{inverse_match} ? 'if' : 'unless'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | my $wrapped = get_code_in_safe_env("{; ${use_stmt} undef \$_ ${conjunction} m ${quoted_re} }", | |||||||||||||||||||||||||||||||||||||||||
| 267 | $options, '--grep'); | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | local $. = 0; | |||||||||||||||||||||||||||||||||||||||||
| 269 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | $m_setter->set(\$markers->[$.]); | |||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | $n_setter->set($.++); | |||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | $wrapped->() | |||||||||||||||||||||||||||||||||||||||||
| 273 | } @$content; | ||||||||||||||||||||||||||||||||||||||||||
| 274 | # This code is duplicated from do_perl: | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | for my $i (0 .. $#$content) { | |||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | if (not defined $content->[$i]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | undef $markers->[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 278 | } elsif (not defined $markers->[$i]) { | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | $markers->[$i] = ''; # We don't want undef here, as we will filter on it. | |||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | @$content = grep { defined } @$content; | |||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | @$markers = grep { defined } @$markers; | |||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | sub do_substitute { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_substitute: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | my ($content, $markers, $modes, $options, $re, $subst) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | ▶ | if ($options->{debug_mode}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | print "Before: \$re = ${re}; \$subst = ${subst}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | my ($use_stmt, $quoted_re) = prepare_re2($re, $modes); | |||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | my $quoted_subst = quote_for_re($subst, $modes); | |||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | ▶ | my $g = $modes->{global_match} ? 'g' : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | ▶ | if ($options->{debug_mode}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | print "After: \$re = ${quoted_re}; \$subst = ${quoted_subst}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | my $wrapped = get_code_in_safe_env("; ${use_stmt} s ${quoted_re}{${quoted_subst}}${g}", | |||||||||||||||||||||||||||||||||||||||||
| 298 | $options, '--substitute'); | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | local $. = 0; | |||||||||||||||||||||||||||||||||||||||||
| 300 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | $m_setter->set(\$markers->[$.]); | |||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | $n_setter->set($.++); | |||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | $wrapped->() | |||||||||||||||||||||||||||||||||||||||||
| 304 | } @$content; | ||||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | ▶ | sub warn_or_die_if_needed { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
warn_or_die_if_needed: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | my ($text, $modes) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | ▶ | return 0 unless $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | chomp($@); | |||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | ▶ | if ($modes->{fatal_error}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | die "FATAL: ${text}: ${@}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 313 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | print "WARNING: ${text}: ${@}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | ▶ | sub eval_in_safe_env { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
eval_in_safe_env: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | my ($code, $options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | ▶ | if ($options->{debug_mode} > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | print "Evaluating the following code: ${code}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | ▶ | if ($options->{use_safe} > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | return $safe->reval($code); | |||||||||||||||||||||||||||||||||||||||||
| 326 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | return eval("package App::PTP::SafeEnv; no strict; no warnings; ${code}"); ## no critic (ProhibitStringyEval) | |||||||||||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | ▶ | sub get_code_in_safe_env { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_code_in_safe_env: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | my ($code, $options, $cmd) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | my $wrapped_code = eval_in_safe_env("sub { ${code} }", $options); | |||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | ▶ | die "FATAL: Cannot wrap code for ${cmd}: ${@}" if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | return $wrapped_code; | |||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | # Returns a modified version of $code with the effect of the modes currently | ||||||||||||||||||||||||||||||||||||||||||
| 339 | # in effect applied. | ||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | ▶ | sub prepare_code { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_code: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | my ($code, $modes) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | ▶ | $code =~ s/\Q$modes->{single_quote}/'/g if exists $modes->{single_quote}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | ▶ | $code =~ s/\Q$modes->{double_quote}/"/g if exists $modes->{double_quote}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | ▶ | $code =~ s/\Q$modes->{dollar_sigil}/\$/g if exists $modes->{dollar_sigil}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | return $code; | |||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | ▶ | sub do_perl { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_perl: not called CC=15 SCAR=54.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | my ($content, $markers, $modes, $options, $cmd, $code) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | local $. = 0; | |||||||||||||||||||||||||||||||||||||||||
| 351 | 0 | my $scmd = '-'.($cmd =~ s/^(..)/-$1/r); # --perl or -n. | |||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | my $wrapped_code = get_code_in_safe_env(prepare_code($code, $modes), $options, $scmd); | |||||||||||||||||||||||||||||||||||||||||
| 353 | my @result = map { | ||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | $m_setter->set(\$markers->[$.]); | |||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | $n_setter->set(++$.); | |||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | my $input = $_; | |||||||||||||||||||||||||||||||||||||||||
| 357 | # Among other things, this ensures that the code is always executed in | ||||||||||||||||||||||||||||||||||||||||||
| 358 | # a scalar context. | ||||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | my $r = eval { $wrapped_code->() }; | |||||||||||||||||||||||||||||||||||||||||
| 360 | # We can't use return as we're not in a sub. | ||||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | ▶ | if (warn_or_die_if_needed("Perl code failed in ${scmd}", $modes)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | ▶ | if ($cmd eq 'filter') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 364 | } elsif ($cmd eq 'n') { | ||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | $input; | |||||||||||||||||||||||||||||||||||||||||
| 366 | } elsif ($cmd eq 'mark-line') { | ||||||||||||||||||||||||||||||||||||||||||
| 367 | 0 | $markers[$.]; | |||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||
| 369 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | $r; | |||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||
| 372 | } @$content; | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | $n_setter->set(undef); | |||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | $m_setter->set(\undef); | |||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | ▶ | if ($cmd eq 'perl') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 378 | # Do nothing with the result. | ||||||||||||||||||||||||||||||||||||||||||
| 379 | } elsif ($cmd eq 'n') { | ||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | @$content = @result; | |||||||||||||||||||||||||||||||||||||||||
| 381 | } elsif ($cmd eq 'filter') { | ||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | for my $i (0 .. $#$content) { | |||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | ▶ | if (!$result[$i] xor $modes->{inverse_match}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/4
not $result[$i] xor $modes->{"inverse_match"}
MC/DC0%0/2
not $result[$i] xor $modes->{"inverse_match"}
not $result[$i]
$modes->{"inverse_match"}
Truth table0%0/4
not(A) xor B
A= not $result[$i]B= $modes->{"inverse_match"}
| |||||||||||||||||||||||||||||||||||||||||||
| 384 | 0 | undef $content->[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||||||||||||||||
| 387 | } elsif ($cmd eq 'mark-line') { | ||||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | @$markers = @result; | |||||||||||||||||||||||||||||||||||||||||
| 389 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | die "FATAL: Invalid command received for perl operation ($cmd).\n"; | |||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | for my $i (0 .. $#$content) { | |||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | ▶ | if (not defined $content->[$i]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 395 | 0 | undef $markers->[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 396 | } elsif (not defined $markers->[$i]) { | ||||||||||||||||||||||||||||||||||||||||||
| 397 | # We don't want undef here, as we will filter on it. | ||||||||||||||||||||||||||||||||||||||||||
| 398 | # Note that the $m and @m variables shared with the environment try to | ||||||||||||||||||||||||||||||||||||||||||
| 399 | # prevent the user from setting undef in them. But we don't trust that | ||||||||||||||||||||||||||||||||||||||||||
| 400 | # Too much. | ||||||||||||||||||||||||||||||||||||||||||
| 401 | 0 | $markers->[$i] = ''; | |||||||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | @$content = grep { defined } @$content; | |||||||||||||||||||||||||||||||||||||||||
| 405 | 0 | @$markers = grep { defined } @$markers; | |||||||||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | ▶ | sub do_execute { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_execute: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | my ($content, $markers, $modes, $options, $cmd, $code) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | ▶ | $code = prepare_code($code, $modes) if $cmd ne 'M'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | ▶ | $code = "use $code;" if $cmd eq 'M'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | eval_in_safe_env($code, $options); | |||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | chomp($@); | |||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | my $scmd = '-'.($cmd =~ s/^(..)/-$1/r); # --execute or -M. | |||||||||||||||||||||||||||||||||||||||||
| 416 | 0 | die "FATAL: Perl code failed in ${scmd}: ${@}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | 0 | ▶ | sub do_load { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_load: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 421 | 0 | my ($content, $markers, $modes, $options, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 422 | # do can open relative paths, but in that case it looks them up in the @INC | ||||||||||||||||||||||||||||||||||||||||||
| 423 | # directory, which we want to avoid. | ||||||||||||||||||||||||||||||||||||||||||
| 424 | # We don't use abs_path here to not die (just yet) if the file does not exist. | ||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | my $abs_path = rel2abs($file); | |||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | ▶ | print "Loading file: '$abs_path'\n" if $options->{debug_mode}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 427 | 0 | ▶ | if (not defined eval_in_safe_env("do '${abs_path}';", $options)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 428 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | die "FATAL: Perl code failed in --load: ${@}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 430 | } elsif ($!) { | ||||||||||||||||||||||||||||||||||||||||||
| 431 | 0 | die "FATAL: Cannot load file '$file' for --load: $!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||
| 436 | # Notes: we are not applying the --sq, --dq, and --ds mode to the code passed | ||||||||||||||||||||||||||||||||||||||||||
| 437 | # in the --comparator mode, as that is not a command. | ||||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | ▶ | sub do_sort { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_sort: not called CC=12 SCAR=47.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 439 | 0 | my ($content, $markers, $modes, $options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 440 | 0 | ▶ | if (ref($modes->{comparator}) eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 441 | # This branch is no longer used. | ||||||||||||||||||||||||||||||||||||||||||
| 442 | 0 | @$content = sort { $modes->{comparator}() } @$content; | |||||||||||||||||||||||||||||||||||||||||
| 443 | } elsif (ref($modes->{comparator}) eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | ▶ | if (${$modes->{comparator}} eq 'default') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 445 | 0 | @$content = sort @$content; | |||||||||||||||||||||||||||||||||||||||||
| 446 | 0 | } elsif (${$modes->{comparator}} eq 'numeric') { | |||||||||||||||||||||||||||||||||||||||||
| 447 | 2 | ▶ | no warnings 'numeric'; ## no critic (ProhibitNoWarnings) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 448 | 0 | @$content = sort { $a <=> $b } @$content; | |||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | } elsif (${$modes->{comparator}} eq 'locale') { | |||||||||||||||||||||||||||||||||||||||||
| 450 | 2 | ▶ | use locale; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 451 | 0 | @$content = sort { $a cmp $b } @$content; | |||||||||||||||||||||||||||||||||||||||||
| 452 | 0 | } elsif (${$modes->{comparator}} eq 'semver') { | |||||||||||||||||||||||||||||||||||||||||
| 453 | 0 | my (%parsed, %warned); | |||||||||||||||||||||||||||||||||||||||||
| 454 | 0 | for my $line (@$content) { | |||||||||||||||||||||||||||||||||||||||||
| 455 | 0 | ▶ | next if exists $parsed{$line}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | my $version = App::PTP::Util::Semver::parse($line); | |||||||||||||||||||||||||||||||||||||||||
| 457 | 0 | $parsed{$line} = $version; | |||||||||||||||||||||||||||||||||||||||||
| 458 | 0 | for my $warning (@{$version->{warnings}}) { | |||||||||||||||||||||||||||||||||||||||||
| 459 | 0 | ▶ | print "WARNING: ${warning}\n" unless $warned{$warning}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||||||||||||||||
| 462 | @$content = | ||||||||||||||||||||||||||||||||||||||||||
| 463 | 0 | sort { App::PTP::Util::Semver::compare_parsed($parsed{$a}, $parsed{$b}) } @$content; | |||||||||||||||||||||||||||||||||||||||||
| 464 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 465 | 0 | die sprintf "INTERNAL ERROR: Invalid comparator (%s)\n", ${$modes->{comparator}}; | |||||||||||||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||||||||||||
| 467 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 468 | die sprintf "INTERNAL ERROR: Invalid comparator type (%s)\n", Dumper($modes->{comparator}) | ||||||||||||||||||||||||||||||||||||||||||
| 469 | 0 | ▶ | if ref $modes->{comparator}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 470 | 0 | my $cmp = $modes->{comparator}; | |||||||||||||||||||||||||||||||||||||||||
| 471 | 0 | my $sort = get_code_in_safe_env("sort { $cmp } \@_", $options, 'custom comparator'); | |||||||||||||||||||||||||||||||||||||||||
| 472 | 0 | @$content = $sort->(@$content); | |||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||
| 474 | 0 | @$markers = (0) x scalar(@$content); | |||||||||||||||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||
| 477 | 0 | ▶ | sub do_list_op { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_list_op: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 478 | 0 | my ($content, $markers, $modes, $options, $sub, $apply_on_markers) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 479 | 0 | ▶ | if ($apply_on_markers eq 'none') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 480 | 0 | @$content = &$sub(@$content); | |||||||||||||||||||||||||||||||||||||||||
| 481 | 0 | @$markers = (0) x scalar(@$content); | |||||||||||||||||||||||||||||||||||||||||
| 482 | } elsif ($apply_on_markers eq 'same') { | ||||||||||||||||||||||||||||||||||||||||||
| 483 | 0 | @$content = &$sub(@$content); | |||||||||||||||||||||||||||||||||||||||||
| 484 | 0 | @$markers = &$sub(@$markers); | |||||||||||||||||||||||||||||||||||||||||
| 485 | } elsif ($apply_on_markers eq 'together') { | ||||||||||||||||||||||||||||||||||||||||||
| 486 | 0 | &$sub($content, $markers); | |||||||||||||||||||||||||||||||||||||||||
| 487 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 488 | 0 | die | |||||||||||||||||||||||||||||||||||||||||
| 489 | "INTERNAL ERROR: Invalid value for \$apply_on_markers passed to do_list_op: $apply_on_markers\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 490 | } | ||||||||||||||||||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||
| 493 | 2 | Readonly::Scalar my $DEFAULT_TAIL_LEN => 10; | |||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||
| 495 | 0 | ▶ | sub do_tail { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_tail: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 496 | 0 | my ($content, $markers, $modes, $options, $len) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 497 | 0 | ▶ | $len = $DEFAULT_TAIL_LEN unless $len; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 498 | 0 | splice @$content, 0, -$len; | |||||||||||||||||||||||||||||||||||||||||
| 499 | 0 | splice @$markers, 0, -$len; | |||||||||||||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | 0 | ▶ | sub do_head { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_head: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 503 | 0 | my ($content, $markers, $modes, $options, $len) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 504 | 0 | ▶ | $len = $DEFAULT_TAIL_LEN unless $len; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 505 | 0 | ▶ | $len = -@$content if $len < -@$content; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 506 | 0 | splice @$content, $len; | |||||||||||||||||||||||||||||||||||||||||
| 507 | 0 | splice @$markers, $len; | |||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | 0 | ▶ | sub do_delete_marked { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_delete_marked: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 511 | # negative offset if we're deleting a line before the marker. | ||||||||||||||||||||||||||||||||||||||||||
| 512 | 0 | my ($content, $markers, $modes, $options, $offset) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 513 | 0 | my $start = min(max(0, -$offset), $#$content); | |||||||||||||||||||||||||||||||||||||||||
| 514 | 0 | my $end = max(min($#$content, $#$content + $offset), $#$content); | |||||||||||||||||||||||||||||||||||||||||
| 515 | 0 | my @markers_temp = @$markers; # So that we can read it even after an undef. | |||||||||||||||||||||||||||||||||||||||||
| 516 | 0 | for my $i ($start .. $end) { | |||||||||||||||||||||||||||||||||||||||||
| 517 | 0 | ▶ | if ($markers_temp[$i]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 518 | 0 | undef $content->[$i + $offset]; | |||||||||||||||||||||||||||||||||||||||||
| 519 | 0 | undef $markers->[$i + $offset]; | |||||||||||||||||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||||||||||||||||
| 521 | } | ||||||||||||||||||||||||||||||||||||||||||
| 522 | 0 | @$content = grep { defined } @$content; | |||||||||||||||||||||||||||||||||||||||||
| 523 | 0 | @$markers = grep { defined } @$markers; | |||||||||||||||||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||
| 526 | 0 | ▶ | sub do_insert_marked { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_insert_marked: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 527 | # negative offset if we're inserting a line before the marker. | ||||||||||||||||||||||||||||||||||||||||||
| 528 | 0 | my ($content, $markers, $modes, $options, $offset, $line) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 529 | 0 | my @markers_temp = @$markers; | |||||||||||||||||||||||||||||||||||||||||
| 530 | 0 | my @content_temp = @$content; | |||||||||||||||||||||||||||||||||||||||||
| 531 | 0 | my $added = 0; | |||||||||||||||||||||||||||||||||||||||||
| 532 | 0 | my $wrapped; | |||||||||||||||||||||||||||||||||||||||||
| 533 | 0 | ▶ | if (not $modes->{quote_regex}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 534 | 0 | $wrapped = get_code_in_safe_env("\"${line}\"", $options, '--insert-marked'); | |||||||||||||||||||||||||||||||||||||||||
| 535 | } | ||||||||||||||||||||||||||||||||||||||||||
| 536 | 0 | for my $i (0 .. $#content_temp) { | |||||||||||||||||||||||||||||||||||||||||
| 537 | 0 | ▶ | next unless $markers_temp[$i]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 538 | 0 | my $r; | |||||||||||||||||||||||||||||||||||||||||
| 539 | 0 | ▶ | if ($modes->{quote_regex}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 540 | 0 | $r = $line; | |||||||||||||||||||||||||||||||||||||||||
| 541 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 542 | 0 | $_ = $content_temp[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 543 | 0 | $n_setter->set($i + 1); | |||||||||||||||||||||||||||||||||||||||||
| 544 | 0 | $m_setter->set($markers_temp[$i]); | |||||||||||||||||||||||||||||||||||||||||
| 545 | 0 | $line =~ s/(?:[^\\]|^)((:?\\\\)*")/\\$1/g; | |||||||||||||||||||||||||||||||||||||||||
| 546 | 0 | $r = eval { $wrapped->() }; | |||||||||||||||||||||||||||||||||||||||||
| 547 | 0 | ▶ | next if warn_or_die_if_needed('String interpolation failed in --insert-marked', $modes); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 548 | # it should not be possible for the result to be undef... | ||||||||||||||||||||||||||||||||||||||||||
| 549 | } | ||||||||||||||||||||||||||||||||||||||||||
| 550 | # We never insert at a negative offset or before the previously added line. | ||||||||||||||||||||||||||||||||||||||||||
| 551 | # Offset = 0 means we're inserting after the current line. | ||||||||||||||||||||||||||||||||||||||||||
| 552 | 0 | my $target = $i + $offset + 1 + $added; | |||||||||||||||||||||||||||||||||||||||||
| 553 | 0 | ▶ | $target = $added if $target < $added; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 554 | 0 | ▶ | $target = @$content if $target > @$content; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 555 | 0 | ++$added; | |||||||||||||||||||||||||||||||||||||||||
| 556 | 0 | splice @$content, $target, 0, $r; | |||||||||||||||||||||||||||||||||||||||||
| 557 | 0 | splice @$markers, $target, 0, 0; | |||||||||||||||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||||||||||||||||
| 559 | } | ||||||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||||||
| 561 | 0 | ▶ | sub do_set_markers { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_set_markers: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 562 | 0 | my ($content, $markers, $modes, $options, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 563 | 0 | @$markers = ($value) x scalar(@$content); | |||||||||||||||||||||||||||||||||||||||||
| 564 | } | ||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||
| 566 | 0 | ▶ | sub do_number_lines { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_number_lines: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 567 | 0 | my ($content, $markers, $modes, $options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 568 | 0 | my $line = 0; | |||||||||||||||||||||||||||||||||||||||||
| 569 | 0 | my $n = int(log(@$content) / log(10)) + 1; ## no critic (ProhibitMagicNumbers) | |||||||||||||||||||||||||||||||||||||||||
| 570 | 0 | map { $_ = sprintf("%${n}d %s", ++$line, $_) } @$content; | |||||||||||||||||||||||||||||||||||||||||
| 571 | } | ||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||
| 573 | 0 | ▶ | sub do_file_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_file_name: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 574 | 0 | my ($content, $markers, $modes, $options, $replace_all) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 575 | 0 | my $name; | |||||||||||||||||||||||||||||||||||||||||
| 576 | 0 | ▶ | if (ref($modes->{file_name_ref}) eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 577 | 0 | $name = ${$modes->{file_name_ref}}; | |||||||||||||||||||||||||||||||||||||||||
| 578 | ▶ | } elsif (ref($modes->{file_name_ref}) eq 'REF' | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $modes->{"file_name_ref"} eq "REF" and ref ${$modes->{"file_name_ref"};} eq "SCALAR"
MC/DC0%0/2
ref $modes->{"file_name_ref"} eq "REF" and ref ${$modes->{"file_name_ref"};} eq "SCALAR"
ref $modes->{"file_name_ref"} eq "REF"
ref ${$modes->{"file_name_ref"};} eq "SCALAR"
Truth table0%0/3
A and B
A= ref $modes->{"file_name_ref"} eq "REF"B= ref ${$modes->{"file_name_ref"};} eq "SCALAR"
| |||||||||||||||||||||||||||||||||||||||||||
| 579 | 0 | and ref(${$modes->{file_name_ref}}) eq 'SCALAR') { | |||||||||||||||||||||||||||||||||||||||||
| 580 | 0 | $name = $${$modes->{file_name_ref}}; | |||||||||||||||||||||||||||||||||||||||||
| 581 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 582 | 0 | die 'INTERNAL ERROR: Invalid input marker: '.Dumper($modes->{file_name_ref})."\n"; | |||||||||||||||||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||||||||||||||||
| 584 | 0 | ▶ | if ($replace_all) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 585 | # Does nothing to empty file. | ||||||||||||||||||||||||||||||||||||||||||
| 586 | 0 | ▶ | if (@$content) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 587 | 0 | @$content = ($name); | |||||||||||||||||||||||||||||||||||||||||
| 588 | 0 | @$markers = (0); | |||||||||||||||||||||||||||||||||||||||||
| 589 | } | ||||||||||||||||||||||||||||||||||||||||||
| 590 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 591 | 0 | unshift @$content, $name; | |||||||||||||||||||||||||||||||||||||||||
| 592 | 0 | unshift @$markers, 0; | |||||||||||||||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||||||||||||||||
| 594 | } | ||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||
| 596 | 0 | ▶ | sub do_line_count { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_line_count: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 597 | 0 | my ($content, $markers, $modes, $options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 598 | 0 | @$content = (scalar(@$content)); | |||||||||||||||||||||||||||||||||||||||||
| 599 | 0 | @$markers = (0); | |||||||||||||||||||||||||||||||||||||||||
| 600 | } | ||||||||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||||||||
| 602 | 0 | ▶ | sub do_cut { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_cut: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 603 | 0 | my ($content, $markers, $modes, $options, $spec) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 604 | 0 | my $re = prepare_re($modes->{input_field}, $modes); | |||||||||||||||||||||||||||||||||||||||||
| 605 | 0 | ▶ | if ($options->{debug_mode}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 606 | 0 | print "Examples of the --cut operation:\n"; | |||||||||||||||||||||||||||||||||||||||||
| 607 | 0 | Readonly::Scalar my $MAX_LINES_TO_PRINT => 5; | |||||||||||||||||||||||||||||||||||||||||
| 608 | 0 | my @debug = map { [split $re] } @$content[0 .. min($MAX_LINES_TO_PRINT, $#$content)]; | |||||||||||||||||||||||||||||||||||||||||
| 609 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 610 | 0 | ▶ | for ((@$_)[@$spec]) { $_ = "-->${_}<--" if $_ } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 611 | } @debug; | ||||||||||||||||||||||||||||||||||||||||||
| 612 | 0 | local $, = $modes->{output_field}; | |||||||||||||||||||||||||||||||||||||||||
| 613 | 0 | local $\ = $options->{output_separator}; | |||||||||||||||||||||||||||||||||||||||||
| 614 | 0 | map { print @$_ } @debug; | |||||||||||||||||||||||||||||||||||||||||
| 615 | } | ||||||||||||||||||||||||||||||||||||||||||
| 616 | @$content = | ||||||||||||||||||||||||||||||||||||||||||
| 617 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 618 | 0 | join $modes->{output_field}, | |||||||||||||||||||||||||||||||||||||||||
| 619 | 0 | ▶ | map { $_ ? $_ : '' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 620 | (split $re)[@$spec] | ||||||||||||||||||||||||||||||||||||||||||
| 621 | } @$content; | ||||||||||||||||||||||||||||||||||||||||||
| 622 | } | ||||||||||||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||||||||||||
| 624 | 0 | ▶ | sub do_paste { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_paste: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 625 | 0 | my ($content, $markers, $modes, $options, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 626 | 0 | my ($side_content, undef) = read_side_input($file, $options); | |||||||||||||||||||||||||||||||||||||||||
| 627 | 0 | for my $i (0 .. $#$side_content) { | |||||||||||||||||||||||||||||||||||||||||
| 628 | 0 | $content->[$i] .= $modes->{output_field}.$side_content->[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 629 | } | ||||||||||||||||||||||||||||||||||||||||||
| 630 | 0 | for my $i ($#$side_content + 1 .. $#$content) { | |||||||||||||||||||||||||||||||||||||||||
| 631 | 0 | $content->[$i] .= $modes->{output_field}; | |||||||||||||||||||||||||||||||||||||||||
| 632 | } | ||||||||||||||||||||||||||||||||||||||||||
| 633 | 0 | ▶ | if (@$content > @$markers) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 634 | 0 | splice @$markers, scalar(@$markers), 0, (0) x (@$content - @$markers); | |||||||||||||||||||||||||||||||||||||||||
| 635 | } | ||||||||||||||||||||||||||||||||||||||||||
| 636 | } | ||||||||||||||||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||||||||||||||||
| 638 | 0 | ▶ | sub do_pivot { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_pivot: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 639 | 0 | my ($content, $markers, $modes, $options, $action) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 640 | 0 | my $m = 0; | |||||||||||||||||||||||||||||||||||||||||
| 641 | # This is unused by the 'pivot' action, but it's not a huge issue. | ||||||||||||||||||||||||||||||||||||||||||
| 642 | 0 | my $re = prepare_re($modes->{input_field}, $modes); | |||||||||||||||||||||||||||||||||||||||||
| 643 | 0 | ▶ | if ($action eq 'transpose') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 644 | 0 | $m = 0; | |||||||||||||||||||||||||||||||||||||||||
| 645 | my @lines = | ||||||||||||||||||||||||||||||||||||||||||
| 646 | 0 | map { my $r = [split $re]; $m = max($m, scalar(@$r)); $r } @$content; | |||||||||||||||||||||||||||||||||||||||||
| 647 | @$content = | ||||||||||||||||||||||||||||||||||||||||||
| 648 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 649 | 0 | my $c = $_; | |||||||||||||||||||||||||||||||||||||||||
| 650 | 0 | ▶ | join $modes->{output_field}, map { $_->[$c] // '' } @lines; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$_->[$c] // ""
MC/DC0%0/1
$_->[$c] // ""
$_->[$c]
Truth table0%0/2
A
A= $_->[$c]
| |||||||||||||||||||||||||||||||||||||||||||
| 651 | } 0 .. ($m - 1); | ||||||||||||||||||||||||||||||||||||||||||
| 652 | } elsif ($action eq 'pivot') { | ||||||||||||||||||||||||||||||||||||||||||
| 653 | 0 | $m = 1; | |||||||||||||||||||||||||||||||||||||||||
| 654 | 0 | @$content = (join $modes->{output_field}, @$content); | |||||||||||||||||||||||||||||||||||||||||
| 655 | } elsif ($action eq 'anti-pivot') { | ||||||||||||||||||||||||||||||||||||||||||
| 656 | 0 | @$content = map { split $re } @$content; | |||||||||||||||||||||||||||||||||||||||||
| 657 | 0 | $m = @$content; | |||||||||||||||||||||||||||||||||||||||||
| 658 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 659 | 0 | die "INTERNAL ERROR: unknown action for the pivot method: ${action}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 660 | } | ||||||||||||||||||||||||||||||||||||||||||
| 661 | 0 | @$markers = (0) x $m; | |||||||||||||||||||||||||||||||||||||||||
| 662 | } | ||||||||||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||||||||||
| 664 | 0 | ▶ | sub do_tee { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_tee: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 665 | 0 | my ($content, $markers, $modes, $options, $file_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 666 | 0 | $file_name = maybe_interpolate($file_name, $modes, $options, 'tee'); | |||||||||||||||||||||||||||||||||||||||||
| 667 | # This missing_final_separator is not really an option, it is added in the | ||||||||||||||||||||||||||||||||||||||||||
| 668 | # modes struct by the 'process' method, specifically for this function. | ||||||||||||||||||||||||||||||||||||||||||
| 669 | 0 | write_side_output($file_name, $content, $modes->{missing_final_separator}, $options); | |||||||||||||||||||||||||||||||||||||||||
| 670 | } | ||||||||||||||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||||||||||||
| 672 | 0 | ▶ | sub do_shell { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_shell: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 673 | 0 | my ($content, $markers, $modes, $options, $command, $arg) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 674 | 0 | $arg = maybe_interpolate($arg, $modes, $options, prepare_code($command, $modes)); | |||||||||||||||||||||||||||||||||||||||||
| 675 | 0 | ▶ | if ($command eq 'shell') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 676 | 0 | local $SIG{PIPE} = 'IGNORE'; | |||||||||||||||||||||||||||||||||||||||||
| 677 | 0 | ▶ | open(my $pipe, '|-', $arg) or die "FATAL: Cannot execute command given to --${command}: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 678 | 0 | write_handle($pipe, $content, $modes->{missing_final_separator}, $options); | |||||||||||||||||||||||||||||||||||||||||
| 679 | # When run by CPAN testers, this fails sometime for unknown reason. So this | ||||||||||||||||||||||||||||||||||||||||||
| 680 | # is only a warning and not a fatal error. | ||||||||||||||||||||||||||||||||||||||||||
| 681 | 0 | ▶ | close $pipe or print "WARNING: Cannot close pipe for command given to --${command}: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 682 | } elsif ($command eq 'xargs') { | ||||||||||||||||||||||||||||||||||||||||||
| 683 | 0 | for my $i (0 .. $#$content) { | |||||||||||||||||||||||||||||||||||||||||
| 684 | 0 | ▶ | next unless defined $content->[$i]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 685 | 0 | my $cmd = $arg; | |||||||||||||||||||||||||||||||||||||||||
| 686 | 0 | ▶ | $cmd =~ s/{}/$content->[$i]/ or $cmd .= ' '.$content->[$i]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 687 | 0 | ▶ | if ($i == 0 && $options->{debug_mode}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$i == 0 and $options->{"debug_mode"}
MC/DC0%0/2
$i == 0 and $options->{"debug_mode"}
$i == 0
$options->{"debug_mode"}
Truth table0%0/3
A and B
A= $i == 0B= $options->{"debug_mode"}
| |||||||||||||||||||||||||||||||||||||||||||
| 688 | 0 | print "Example of the xargs command: $cmd\n"; | |||||||||||||||||||||||||||||||||||||||||
| 689 | } | ||||||||||||||||||||||||||||||||||||||||||
| 690 | 0 | system($cmd); | |||||||||||||||||||||||||||||||||||||||||
| 691 | } | ||||||||||||||||||||||||||||||||||||||||||
| 692 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 693 | 0 | die "INTERNAL ERROR: Unexpected command in do_shell: ${command}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 694 | } | ||||||||||||||||||||||||||||||||||||||||||
| 695 | 0 | @$content = (); | |||||||||||||||||||||||||||||||||||||||||
| 696 | 0 | @$markers = (); | |||||||||||||||||||||||||||||||||||||||||
| 697 | } | ||||||||||||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||||||||||||
| 699 | 0 | ▶ | sub do_eat { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_eat: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 700 | 0 | my ($content, $markers, $modes, $options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 701 | 0 | @$content = (); | |||||||||||||||||||||||||||||||||||||||||
| 702 | 0 | @$markers = (); | |||||||||||||||||||||||||||||||||||||||||
| 703 | } | ||||||||||||||||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||||||||||||||||
| 705 | 0 | ▶ | sub do_push { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_push: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 706 | 0 | my ($content, $markers, $modes, $options, $command) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 707 | 0 | ▶ | if ($command eq 'push') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 708 | 0 | ▶ | printf "Pushing %d lines on the stack\n", scalar @$content if $options->{debug_mode}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 709 | 0 | push @content_stack, [[@$content], [@$markers]]; | |||||||||||||||||||||||||||||||||||||||||
| 710 | } elsif ($command eq 'pop') { | ||||||||||||||||||||||||||||||||||||||||||
| 711 | 0 | ▶ | die "FATAL: Cannot pop an empty stack\n" unless @content_stack; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 712 | 0 | my ($saved_content, $saved_markers) = @{pop @content_stack}; | |||||||||||||||||||||||||||||||||||||||||
| 713 | 0 | @$content = @$saved_content; | |||||||||||||||||||||||||||||||||||||||||
| 714 | 0 | @$markers = @$saved_markers; | |||||||||||||||||||||||||||||||||||||||||
| 715 | 0 | ▶ | printf "Popping %d lines from the stack\n", scalar @$content if $options->{debug_mode}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 716 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 717 | 0 | die "INTERNAL ERROR: Unexpected command in do_push: ${command}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 718 | } | ||||||||||||||||||||||||||||||||||||||||||
| 719 | } | ||||||||||||||||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||||||||||||||||
| 721 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||