| 1 | package File::Canonicalizer; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | ▶ | use warnings FATAL => 'all'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 2 | ▶ | use English; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 2 | require Exporter; | |||||||||||||
| 9 | 2 | our @ISA = qw(Exporter); | |||||||||||||
| 10 | 2 | our @EXPORT = qw(file_canonicalizer); | |||||||||||||
| 11 | |||||||||||||||
| 12 | 2 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 2 | our $VERSION = '0.11'; | |||||||||||||
| 14 | |||||||||||||||
| 15 | 0 | ▶ | sub file_canonicalizer { | ||||||||||||
|
Subroutine
file_canonicalizer: not called CC=21 SCAR=61.4
Pod
documented
| |||||||||||||||
| 16 | 0 | my ( $inp_file # 1 | |||||||||||||
| 17 | , $out_file # 2 | ||||||||||||||
| 18 | , $remove_comments_started_with_RE # 3 | ||||||||||||||
| 19 | , $replace_adjacent_tabs_and_spaces_with_1_space # 4 | ||||||||||||||
| 20 | , $replace_adjacent_slashes_with_single_slash # 5 | ||||||||||||||
| 21 | , $remove_white_char_from_line_edges # 6 | ||||||||||||||
| 22 | , $remove_empty_lines # 7 | ||||||||||||||
| 23 | , $convert_to_lowercased # 8 | ||||||||||||||
| 24 | , $remove_leading_zeroes # 9 | ||||||||||||||
| 25 | , $sort_lines #10 | ||||||||||||||
| 26 | , $aref_replacements #11 | ||||||||||||||
| 27 | ) = @_ ; | ||||||||||||||
| 28 | |||||||||||||||
| 29 | 0 | my %lines ; | |||||||||||||
| 30 | my $INP; | ||||||||||||||
| 31 | 0 | my $OUT; | |||||||||||||
| 32 | 0 | my $i ; | |||||||||||||
| 33 | 0 | my $replaced_pattern; | |||||||||||||
| 34 | 0 | my $replacement; | |||||||||||||
| 35 | |||||||||||||||
| 36 | 0 | ▶ | unless ($inp_file) { $inp_file = '&STDIN'; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 37 | 0 | ▶ | open ($INP, "<$inp_file") || croak "Error: Can't open file \"$inp_file\" for read: $!"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | 0 | ▶ | unless ($out_file) { $out_file = '&STDOUT'; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | 0 | ▶ | open ($OUT, ">$out_file") || croak "Error: Can't open file \"$out_file\" for write: $!" ; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | |||||||||||||||
| 41 | 0 | ▶ | while (<$INP>) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | { | ||||||||||||||
| 43 | 0 | chomp; | |||||||||||||
| 44 | 0 | ▶ | if ($remove_comments_started_with_RE) { s/$remove_comments_started_with_RE.+$//; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | 0 | ▶ | if ($replace_adjacent_tabs_and_spaces_with_1_space) { s/[ \t]+/ /g; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | 0 | ▶ | if ($replace_adjacent_slashes_with_single_slash) { s#/+#/#g; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | 0 | ▶ | if ($remove_empty_lines) { (/^\s*$/) && next; } | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 48 | 0 | ▶ | if ($remove_white_char_from_line_edges) { s/(^[ \t]*|[ \t]*$)//g; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 49 | 0 | ▶ | if ($convert_to_lowercased) { $_ = lc; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | 0 | ▶ | if ($remove_leading_zeroes) { s/(\W)0+(\d)/$1$2/g; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | |||||||||||||||
| 52 | 0 | ▶ | if (defined $aref_replacements) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | { | ||||||||||||||
| 54 | 0 | $i = 0; | |||||||||||||
| 55 | 0 | REPLACEMENTS: while (1) | |||||||||||||
| 56 | { | ||||||||||||||
| 57 | 0 | ($replaced_pattern, $replacement) = (@{$aref_replacements})[$i++,$i++]; | |||||||||||||
| 58 | 0 | ▶ | (defined $replacement) || last REPLACEMENTS; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 0 | s/$replaced_pattern/$replacement/g; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 0 | ▶ | if ($sort_lines) { $lines{$_} = undef; next; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | 0 | ▶ | print $OUT "$_\n" || croak "Error: Can't write to $out_file: $!"; | ||||||||||||
|
Condition0%0/2
"$_\n" || croak("Error: Can't write to ${out_file}: $!")
MC/DC0%0/1
"$_\n" || croak("Error: Can't write to ${out_file}: $!")
"$_\n"
Truth table0%0/2
A
A= "$_\n"
| |||||||||||||||
| 65 | } | ||||||||||||||
| 66 | |||||||||||||||
| 67 | 0 | ▶ | if ($sort_lines) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | { | ||||||||||||||
| 69 | 0 | for (sort keys %lines) | |||||||||||||
| 70 | 0 | ▶ | { print $OUT "$_\n" || croak "Error: Can't write to $out_file: $!"; } | ||||||||||||
|
Condition0%0/2
"$_\n" || croak("Error: Can't write to ${out_file}: $!")
MC/DC0%0/1
"$_\n" || croak("Error: Can't write to ${out_file}: $!")
"$_\n"
Truth table0%0/2
A
A= "$_\n"
| |||||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 0 | close $OUT; | |||||||||||||
| 74 | 0 | close $INP; | |||||||||||||
| 75 | } # end of 'sub file_canonicalizer' | ||||||||||||||
| 76 | |||||||||||||||
| 77 | 2 | 1; | |||||||||||||
| 78 | |||||||||||||||
| 79 | __END__ | ||||||||||||||