| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::PTP; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 17 |  |  | 17 |  | 205672 | use 5.022; | 
|  | 17 |  |  |  |  | 139 |  | 
| 4 | 17 |  |  | 17 |  | 72 | use strict; | 
|  | 17 |  |  |  |  | 27 |  | 
|  | 17 |  |  |  |  | 319 |  | 
| 5 | 17 |  |  | 17 |  | 68 | use warnings; | 
|  | 17 |  |  |  |  | 28 |  | 
|  | 17 |  |  |  |  | 495 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 17 |  |  | 17 |  | 6795 | use App::PTP::Args; | 
|  | 17 |  |  |  |  | 51 |  | 
|  | 17 |  |  |  |  | 619 |  | 
| 8 | 17 |  |  | 17 |  | 114 | use App::PTP::Commands 'warn_or_die_if_needed'; | 
|  | 17 |  |  |  |  | 74 |  | 
|  | 17 |  |  |  |  | 842 |  | 
| 9 | 17 |  |  | 17 |  | 109 | use App::PTP::Files; | 
|  | 17 |  |  |  |  | 35 |  | 
|  | 17 |  |  |  |  | 840 |  | 
| 10 | 17 |  |  | 17 |  | 92 | use Data::Dumper; | 
|  | 17 |  |  |  |  | 35 |  | 
|  | 17 |  |  |  |  | 682 |  | 
| 11 | 17 |  |  | 17 |  | 97 | use File::Find; | 
|  | 17 |  |  |  |  | 31 |  | 
|  | 17 |  |  |  |  | 691 |  | 
| 12 | 17 |  |  | 17 |  | 86 | use Safe; | 
|  | 17 |  |  |  |  | 30 |  | 
|  | 17 |  |  |  |  | 11374 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | our $VERSION = '1.08'; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | $Data::Dumper::Terse = 1;  # Don't output variable names. | 
| 17 |  |  |  |  |  |  | $Data::Dumper::Sortkeys = 1;  # Sort the content of the hash variables. | 
| 18 |  |  |  |  |  |  | $Data::Dumper::Useqq = 1;  # Use double quote for string (better escaping). | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | my $safe = Safe->new(); | 
| 21 |  |  |  |  |  |  | $safe->deny_only(':subprocess', ':ownprocess', ':others', ':dangerous'); | 
| 22 |  |  |  |  |  |  | $safe->reval('use App::PTP::PerlEnv;'); | 
| 23 |  |  |  |  |  |  | $safe->reval('use File::Spec::Functions qw(:ALL);'); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | # maybe_expand_dirs(filepath) | 
| 26 |  |  |  |  |  |  | # If filepath is a normal file, then returns it as, is. | 
| 27 |  |  |  |  |  |  | # If filepath does not exist, then terminates the program with an error. | 
| 28 |  |  |  |  |  |  | # If filepath is a directory and the $recursive option is not set then | 
| 29 |  |  |  |  |  |  | # terminates the program with an error, otherwise returns the list of all files | 
| 30 |  |  |  |  |  |  | # that it contains. | 
| 31 |  |  |  |  |  |  | sub maybe_expand_dirs { | 
| 32 | 150 |  |  | 150 | 0 | 308 | my ($f, $options) = @_; | 
| 33 | 150 | 100 |  |  |  | 800 | if (ref $f) { | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | # This will be the $stdin_marker reference. | 
| 35 | 125 | 50 |  |  |  | 345 | if ($options->{in_place}) { | 
| 36 | 0 |  |  |  |  | 0 | die "Reading from STDIN is incompatible with the --in-place option.\n"; | 
| 37 |  |  |  |  |  |  | } | 
| 38 | 125 |  |  |  |  | 464 | return $f; | 
| 39 |  |  |  |  |  |  | } elsif (not -e $f) { | 
| 40 | 0 |  |  |  |  | 0 | die "File does not exist: ${f}\n"; | 
| 41 |  |  |  |  |  |  | } elsif (-d _) { | 
| 42 | 1 | 50 |  |  |  | 7 | if (not $options->{recursive}) { | 
| 43 | 0 |  |  |  |  | 0 | die "Input is a directory (did you forget the -R option?): ${f}\n"; | 
| 44 |  |  |  |  |  |  | } | 
| 45 | 1 |  |  |  |  | 3 | my @files; | 
| 46 |  |  |  |  |  |  | my $filter; | 
| 47 | 1 | 50 |  |  |  | 3 | if (defined $options->{input_filter}) { | 
| 48 | 1 |  |  |  |  | 10 | $filter = $safe->reval("sub { $options->{input_filter} }"); | 
| 49 | 1 | 50 |  |  |  | 841 | die "FATAL: Cannot wrap code for --input_filter: ${@}" if $@; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  | find({ | 
| 52 |  |  |  |  |  |  | # Because of the follow option, a stat has already been done on the file, | 
| 53 |  |  |  |  |  |  | # so the '_' magic is guaranteed to work. | 
| 54 |  |  |  |  |  |  | wanted => sub { | 
| 55 | 11 | 100 |  | 11 |  | 279 | if (-f _) { | 
| 56 | 9 |  |  |  |  | 14 | my $f = $_; | 
| 57 | 9 | 50 |  |  |  | 21 | if (defined $filter) { | 
| 58 | 9 |  |  |  |  | 34 | my $r = $filter->(); | 
| 59 | 9 | 100 | 66 |  |  | 4716 | return if warn_or_die_if_needed( | 
| 60 |  |  |  |  |  |  | 'Perl code failed while filtering input') || !$r; | 
| 61 |  |  |  |  |  |  | } | 
| 62 | 2 |  |  |  |  | 56 | push @files, $f; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  | } , | 
| 65 | 1 |  |  |  |  | 166 | follow => 1, | 
| 66 |  |  |  |  |  |  | no_chdir => 1, | 
| 67 |  |  |  |  |  |  | }, $f); | 
| 68 | 1 |  |  |  |  | 28 | return sort @files; | 
| 69 |  |  |  |  |  |  | } else { | 
| 70 |  |  |  |  |  |  | # We assume that everything else is a file. | 
| 71 | 24 |  |  |  |  | 115 | return $f; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | sub process_all { | 
| 76 | 134 |  |  | 134 | 0 | 326 | my ($inputs, $pipeline, $options, $stdin) = @_; | 
| 77 | 134 |  |  |  |  | 511 | $App::PTP::Commands::I_setter->set(1); | 
| 78 | 134 | 100 |  |  |  | 332 | if ($options->{merge}) { | 
| 79 | 4 | 50 |  |  |  | 15 | print "Merging all the inputs.\n" if $options->{debug_mode}; | 
| 80 | 4 |  |  |  |  | 6 | my $missing_final_separator = 0; | 
| 81 | 4 |  |  |  |  | 5 | my @content; | 
| 82 | 4 |  |  |  |  | 10 | for my $input (@$inputs) { | 
| 83 | 7 |  |  |  |  | 15 | my ($content, $missing_separator) = | 
| 84 |  |  |  |  |  |  | read_input($input, $options, $stdin); | 
| 85 | 7 |  |  |  |  | 18 | push @content, @$content; | 
| 86 | 7 |  |  |  |  | 16 | $missing_final_separator = $missing_separator; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  | App::PTP::Commands::process( | 
| 89 | 4 |  |  |  |  | 18 | \$App::PTP::Files::merged_marker, $pipeline, $options, \@content, | 
| 90 |  |  |  |  |  |  | $missing_final_separator); | 
| 91 | 4 |  |  |  |  | 14 | write_output(\$App::PTP::Files::merged_marker, \@content, | 
| 92 |  |  |  |  |  |  | $missing_final_separator, $options); | 
| 93 |  |  |  |  |  |  | } else { | 
| 94 | 130 |  |  |  |  | 276 | for my $file_name (@$inputs) { | 
| 95 | 144 |  |  |  |  | 404 | my ($content, $missing_final_separator) = | 
| 96 |  |  |  |  |  |  | read_input($file_name, $options, $stdin); | 
| 97 |  |  |  |  |  |  | # Note that process can modify the input $file_name variable. | 
| 98 | 144 |  |  |  |  | 596 | App::PTP::Commands::process($file_name, $pipeline, $options, $content, | 
| 99 |  |  |  |  |  |  | $missing_final_separator); | 
| 100 | 142 |  |  |  |  | 2061 | write_output($file_name, $content, $missing_final_separator, $options); | 
| 101 | 142 |  |  |  |  | 517 | $App::PTP::Commands::I_setter->inc(); | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | sub Run { | 
| 107 | 134 |  |  | 134 | 0 | 950 | my ($stdin, $stdout, $stderr, $argv) = @_; | 
| 108 | 134 |  |  |  |  | 392 | select($stderr);  # All debug output, this applies inside the safe too. | 
| 109 | 134 |  |  |  |  | 567 | my ($inputs, $pipeline, $options) = | 
| 110 |  |  |  |  |  |  | App::PTP::Args::parse_command_line($argv); | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 134 | 50 |  |  |  | 379 | if ($options->{debug_mode}) { | 
| 113 | 134 |  |  |  |  | 594 | print 'options = '.Dumper($options)."\n"; | 
| 114 | 134 |  |  |  |  | 16054 | print 'inputs = '.Dumper($inputs)."\n"; | 
| 115 | 134 |  |  |  |  | 5963 | print 'pipeline = '.Dumper($pipeline)."\n"; | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 134 |  |  |  |  | 12276 | @$inputs = map { maybe_expand_dirs($_, $options) } @$inputs; | 
|  | 150 |  |  |  |  | 376 |  | 
| 119 | 134 | 50 |  |  |  | 564 | print 'expanded @inputs = '.Dumper($inputs)."\n" if $options->{debug_mode}; | 
| 120 |  |  |  |  |  |  |  | 
| 121 | 134 | 50 |  |  |  | 5634 | return if $options->{abort}; | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 134 |  |  |  |  | 547 | init_global_output($options, $stdout); | 
| 124 | 134 |  |  |  |  | 19856 | process_all($inputs, $pipeline, $options, $stdin); | 
| 125 | 132 |  |  |  |  | 414 | close_global_output($options); | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | 1; |