| 1 | #!/usr/local/bin/perl -w | ||||||||||||||||||||||||||||
| 2 | # -*- perl -*- | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # Author: Slaven Rezic | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # Copyright (C) 2009,2011,2013,2015,2016,2018,2019,2022,2023 Slaven Rezic. All rights reserved. | ||||||||||||||||||||||||||||
| 8 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 9 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||
| 11 | # Mail: slaven@rezic.de | ||||||||||||||||||||||||||||
| 12 | # WWW: http://www.rezic.de/eserte/ | ||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 98 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 98 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 98 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 101 | $VERSION = "0.12"; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 12 | ▶ | sub _has_capture_groups { | ||||||||||||||||||||||||||
|
Subroutine
_has_capture_groups: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 12 | my $qr = shift; | |||||||||||||||||||||||||||
| 24 | 12 | $qr =~ /(?<!\\)\([^(?:)]/; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 101 | ▶ | return 1 if caller; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 101 | my $sort_type = 'alpha'; | |||||||||||||||||||||||||||
| 30 | 98 | my $reverse; | |||||||||||||||||||||||||||
| 31 | my $check; | ||||||||||||||||||||||||||||
| 32 | 98 | my $unique; | |||||||||||||||||||||||||||
| 33 | 98 | my $ignore_leading_blanks; | |||||||||||||||||||||||||||
| 34 | 98 | my $ignore_case; | |||||||||||||||||||||||||||
| 35 | 98 | my $ignore_nonprinting; | |||||||||||||||||||||||||||
| 36 | 98 | my $perlscript; | |||||||||||||||||||||||||||
| 37 | 98 | my $rx; | |||||||||||||||||||||||||||
| 38 | 98 | my @modules; | |||||||||||||||||||||||||||
| 39 | 98 | my $no_warnings; | |||||||||||||||||||||||||||
| 40 | 98 | my $cmp_perlscript; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 98 | Getopt::Long::Configure("bundling"); | |||||||||||||||||||||||||||
| 43 | GetOptions( | ||||||||||||||||||||||||||||
| 44 | 18 | ▶ | "n|numeric-sort" => sub { $sort_type = 'numeric' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 6 | ▶ | "N|natural-sort" => sub { $sort_type = 'sort_naturally' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 6 | ▶ | "V|version-sort" => sub { $sort_type = 'version' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | "C|compare-function=s" => \$cmp_perlscript, | ||||||||||||||||||||||||||||
| 48 | "b|ignore-leading-blanks" => \$ignore_leading_blanks, | ||||||||||||||||||||||||||||
| 49 | "f|ignore-case" => \$ignore_case, | ||||||||||||||||||||||||||||
| 50 | "i|ignore-nonprinting" => \$ignore_nonprinting, | ||||||||||||||||||||||||||||
| 51 | "r|reverse" => \$reverse, | ||||||||||||||||||||||||||||
| 52 | "c|check" => \$check, | ||||||||||||||||||||||||||||
| 53 | "u|unique" => \$unique, | ||||||||||||||||||||||||||||
| 54 | "e|field-function=s" => \$perlscript, | ||||||||||||||||||||||||||||
| 55 | "rx=s" => \$rx, | ||||||||||||||||||||||||||||
| 56 | 'M|module=s@' => \@modules, | ||||||||||||||||||||||||||||
| 57 | 3 | ▶ | 'm=s@' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 58 | 3 | ▶ | push @modules, $_[1] =~ /=/ ? $_[1] : "$_[1]="; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | }, | ||||||||||||||||||||||||||||
| 60 | "X|no-warnings" => \$no_warnings, | ||||||||||||||||||||||||||||
| 61 | 2 | ▶ | "v|version" => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 2 | print "psort version $VERSION\n"; | |||||||||||||||||||||||||||
| 63 | 2 | exit 0; | |||||||||||||||||||||||||||
| 64 | }, | ||||||||||||||||||||||||||||
| 65 | 98 | ▶ | ) or die <<EOF; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | usage: $0 [OPTION]... [FILE]... | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | Comparison functions: | ||||||||||||||||||||||||||||
| 69 | -C: compare using any perl code (\$a and \$b are defined) | ||||||||||||||||||||||||||||
| 70 | -n: compare numerically | ||||||||||||||||||||||||||||
| 71 | -N: compare using Sort::Naturally | ||||||||||||||||||||||||||||
| 72 | -V: compare versions | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | Sort key selection: | ||||||||||||||||||||||||||||
| 75 | -b: ignore leading blanks | ||||||||||||||||||||||||||||
| 76 | -e: perl oneliner, should return the value to be compared as last value | ||||||||||||||||||||||||||||
| 77 | --rx: capture sort value by regexp | ||||||||||||||||||||||||||||
| 78 | -f: ignore case | ||||||||||||||||||||||||||||
| 79 | -i: ignore non-printing characters | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Misc: | ||||||||||||||||||||||||||||
| 82 | -c: check only | ||||||||||||||||||||||||||||
| 83 | -u: unique | ||||||||||||||||||||||||||||
| 84 | -r: reverse sorting | ||||||||||||||||||||||||||||
| 85 | -M: load perl modules | ||||||||||||||||||||||||||||
| 86 | -m: load perl modules (without importing) | ||||||||||||||||||||||||||||
| 87 | -v: print version | ||||||||||||||||||||||||||||
| 88 | EOF | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 96 | for my $module_spec (@modules) { | |||||||||||||||||||||||||||
| 91 | 9 | my($module,$imports) = split /=/, $module_spec, 2; | |||||||||||||||||||||||||||
| 92 | 9 | eval qq{require $module}; | |||||||||||||||||||||||||||
| 93 | 9 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 9 | my @imports; | |||||||||||||||||||||||||||
| 95 | 9 | ▶ | if (defined $imports) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 6 | ▶ | if (length $imports) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 6 | @imports = split /,/, $imports; | |||||||||||||||||||||||||||
| 98 | 6 | $module->import(@imports); | |||||||||||||||||||||||||||
| 99 | } else { | ||||||||||||||||||||||||||||
| 100 | # don't import anything | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } else { | ||||||||||||||||||||||||||||
| 103 | 3 | $module->import; # default imports | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 96 | ▶ | if ($cmp_perlscript) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 15 | $sort_type = eval "no strict; sub { $cmp_perlscript }"; | |||||||||||||||||||||||||||
| 109 | 15 | ▶ | die "Cannot compile 'cmp' code: $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 96 | my @data; | |||||||||||||||||||||||||||
| 113 | my $cb; | ||||||||||||||||||||||||||||
| 114 | 96 | ▶ | defined $perlscript and defined $rx | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $perlscript and defined $rx
MC/DC0%0/2
defined $perlscript and defined $rx
defined $perlscript
defined $rx
Truth table66%2/3
A and B
A= defined $perlscriptB= defined $rx
| |||||||||||||||||||||||||||||
| 115 | and die "Cannot define -e/--field-function and --rx together\n"; | ||||||||||||||||||||||||||||
| 116 | 96 | ▶ | if (defined $perlscript) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 117 | 9 | $cb = eval "no strict; sub { $perlscript }"; | |||||||||||||||||||||||||||
| 118 | 9 | ▶ | die "Cannot compile code: $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | } elsif ($rx) { | ||||||||||||||||||||||||||||
| 120 | 12 | my $qr = eval { qr{$rx} }; | |||||||||||||||||||||||||||
| 121 | 12 | ▶ | die "Cannot compile regular expression '$rx': $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 12 | ▶ | if (!_has_capture_groups($qr)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 3 | $qr = eval { qr{($rx)} }; | |||||||||||||||||||||||||||
| 124 | 3 | ▶ | die $@ if $@; # should not happen | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 12 | ▶ | if ($rx =~ m{^/(.*)/$}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 3 | my $rx_without_slashes = $1; | |||||||||||||||||||||||||||
| 128 | 3 | my $warned_about_slashes; | |||||||||||||||||||||||||||
| 129 | 9 | ▶ | $cb = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||
| 130 | 9 | $_ =~ $qr; | |||||||||||||||||||||||||||
| 131 | 9 | ▶ | if (!$warned_about_slashes && !defined $1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$warned_about_slashes or defined $1
MC/DC0%0/2
$warned_about_slashes or defined $1
$warned_about_slashes
defined $1
Truth table33%1/3
A or B
A= $warned_about_slashesB= defined $1
| |||||||||||||||||||||||||||||
| 132 | 0 | warn "No regexp match. Maybe you want to omit the slashes, i.e. use --rx '$rx_without_slashes'?\n"; | |||||||||||||||||||||||||||
| 133 | 0 | $warned_about_slashes = 1; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 9 | $1; | |||||||||||||||||||||||||||
| 136 | 3 | }; | |||||||||||||||||||||||||||
| 137 | } else { | ||||||||||||||||||||||||||||
| 138 | 9 | ▶ | $cb = sub { $_ =~ $qr; $1 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } else { | ||||||||||||||||||||||||||||
| 141 | 75 | ▶ | $cb = sub { $_ }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 96 | ▶ | if (@ARGV) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 96 | for my $file (@ARGV) { | |||||||||||||||||||||||||||
| 146 | 96 | add_psort($file); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | } else { | ||||||||||||||||||||||||||||
| 149 | 0 | add_psort(\*STDIN); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 96 | my $sort_preamble = q{no warnings 'uninitialized';}; | |||||||||||||||||||||||||||
| 153 | 96 | my $sort_creator; | |||||||||||||||||||||||||||
| 154 | 96 | ▶ | if ($sort_type eq 'numeric') { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 155 | 18 | $sort_preamble = q{no warnings 'numeric', 'uninitialized';}; | |||||||||||||||||||||||||||
| 156 | 18 | ▶ | $sort_creator = sub { $_[0] . ' <=> ' . $_[1] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 157 | } elsif ($sort_type eq 'alpha') { | ||||||||||||||||||||||||||||
| 158 | 51 | ▶ | $sort_creator = sub { $_[0] . ' cmp ' . $_[1] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 159 | } elsif ($sort_type eq 'sort_naturally') { | ||||||||||||||||||||||||||||
| 160 | 6 | require Sort::Naturally; | |||||||||||||||||||||||||||
| 161 | 6 | ▶ | $sort_creator = sub { 'Sort::Naturally::ncmp(' . $_[0] . ', ' . $_[1] . ')' }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 162 | } elsif ($sort_type eq 'version') { | ||||||||||||||||||||||||||||
| 163 | 6 | require CPAN::Version; | |||||||||||||||||||||||||||
| 164 | 6 | ▶ | $sort_creator = sub { 'CPAN::Version->vcmp(' . $_[0] . ', ' . $_[1] . ')' }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 165 | } elsif (UNIVERSAL::isa($sort_type, 'CODE')) { | ||||||||||||||||||||||||||||
| 166 | 15 | ▶ | $sort_creator = sub { 'local($a, $b) = (' . $_[0] . ', ' . $_[1] . '); $sort_type->()' }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 167 | } else { | ||||||||||||||||||||||||||||
| 168 | 0 | die "Unhandled sort type '$sort_type'"; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 96 | ▶ | if ($check) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 64 | ▶ | exit 0 if !@data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 62 | my $code = $sort_preamble; | |||||||||||||||||||||||||||
| 174 | 62 | $code .= 'sub { '; | |||||||||||||||||||||||||||
| 175 | 62 | ▶ | if ($reverse) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 8 | $code .= $sort_creator->('$_[1]->[1]', '$_[0]->[1]'); | |||||||||||||||||||||||||||
| 177 | } else { | ||||||||||||||||||||||||||||
| 178 | 54 | $code .= $sort_creator->('$_[0]->[1]', '$_[1]->[1]'); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 62 | $code .= '}'; | |||||||||||||||||||||||||||
| 181 | 62 | ▶ | my $check_sub = eval $code; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 182 | 62 | ▶ | die "Error while evaluating '$code': $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 62 | ▶ | for(my $i=1; $i<=$#data; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 151 | ▶ | if ($unique && $data[$i-1][0] eq $data[$i][0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$unique and $data[$i - 1][0] eq $data[$i][0]
MC/DC0%0/2
$unique and $data[$i - 1][0] eq $data[$i][0]
$unique
$data[$i - 1][0] eq $data[$i][0]
Truth table66%2/3
A and B
A= $uniqueB= $data[$i - 1][0] eq $data[$i][0]
| |||||||||||||||||||||||||||||
| 185 | 0 | exit 1; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | 151 | ▶ | if ($check_sub->($data[$i-1], $data[$i]) > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 24 | exit 1; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | 38 | exit 0; | |||||||||||||||||||||||||||
| 192 | } else { | ||||||||||||||||||||||||||||
| 193 | 32 | my $code = $sort_preamble; | |||||||||||||||||||||||||||
| 194 | 32 | $code .= '@data = '; | |||||||||||||||||||||||||||
| 195 | 32 | ▶ | if ($unique) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 3 | $code .= 'do { my $last_line; grep { my $keep = !defined $last_line || $_->[0] ne $last_line; $last_line = $_->[0]; $keep } '; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | 32 | $code .= 'sort { '; | |||||||||||||||||||||||||||
| 199 | 32 | ▶ | if ($reverse) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 4 | $code .= $sort_creator->('$b->[1]', '$a->[1]'); | |||||||||||||||||||||||||||
| 201 | } else { | ||||||||||||||||||||||||||||
| 202 | 28 | $code .= $sort_creator->('$a->[1]', '$b->[1]'); | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | 32 | $code .= ' } @data'; | |||||||||||||||||||||||||||
| 205 | 32 | ▶ | if ($unique) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 3 | $code .= ' }'; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | 32 | ▶ | eval $code; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 209 | 32 | ▶ | die "Error while evaluating '$code': $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 32 | for (@data) { | |||||||||||||||||||||||||||
| 212 | 140 | print $_->[0]; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 96 | ▶ | sub add_psort { | ||||||||||||||||||||||||||
|
Subroutine
add_psort: called CC=10 SCAR=26.6
| |||||||||||||||||||||||||||||
| 217 | 96 | my($file_or_fh) = @_; | |||||||||||||||||||||||||||
| 218 | 96 | my($fh, $diag); | |||||||||||||||||||||||||||
| 219 | 96 | ▶ | if (ref $file_or_fh eq 'GLOB') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 0 | $fh = $file_or_fh; | |||||||||||||||||||||||||||
| 221 | 0 | ▶ | if ($file_or_fh == \*STDIN) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 0 | $diag = 'standard input'; | |||||||||||||||||||||||||||
| 223 | } else { | ||||||||||||||||||||||||||||
| 224 | 0 | $diag = 'provided file handle'; # currently cannot happen | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | } else { | ||||||||||||||||||||||||||||
| 227 | 96 | my $file = $file_or_fh; | |||||||||||||||||||||||||||
| 228 | 96 | $diag = $file; | |||||||||||||||||||||||||||
| 229 | 96 | ▶ | open $fh, '<', $file or die "Can't open $file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 96 | ▶ | while(<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | 424 | my $line = $_; | |||||||||||||||||||||||||||
| 234 | 424 | my $res = $cb->($_); # force scalar context | |||||||||||||||||||||||||||
| 235 | 424 | ▶ | if (!defined $res) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 0 | ▶ | if (!$no_warnings) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 0 | warn "Uninitialized value returned in psort eval or regexp at $diag line $.\n"; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | 0 | $res = ''; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | 424 | ▶ | $res = uc $res if $ignore_case; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | 424 | ▶ | $res =~ s{^\s+}{} if $ignore_leading_blanks; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | 424 | ▶ | $res =~ s{[[:^print:]]}{}g if $ignore_nonprinting; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | 424 | $res =~ s{\r?\n$}{}; # remove newline at end | |||||||||||||||||||||||||||
| 245 | 424 | push @data, [$line, $res]; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | __END__ | ||||||||||||||||||||||||||||