| 1 | package App::WRT::Util; | ||||||||||||
| 2 | |||||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 11 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 11 | ▶ | use open qw(:std :utf8); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 11 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 11 | ▶ | use Encode; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 11 | ▶ | use base qw(Exporter); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 11 | our @EXPORT_OK = qw(dir_list file_put_contents file_get_contents); | |||||||||||
| 12 | |||||||||||||
| 13 | =over | ||||||||||||
| 14 | |||||||||||||
| 15 | =item dir_list($dir, $sort_order, $pattern) | ||||||||||||
| 16 | |||||||||||||
| 17 | Return a $sort_order sorted list of files matching regex $pattern in a | ||||||||||||
| 18 | directory. | ||||||||||||
| 19 | |||||||||||||
| 20 | Calls $sort_order, which can be one of: | ||||||||||||
| 21 | |||||||||||||
| 22 | alpha - alphabetical | ||||||||||||
| 23 | reverse_alpha - alphabetical, reversed | ||||||||||||
| 24 | high_to_low - numeric, high to low | ||||||||||||
| 25 | low_to_high - numeric, low to high | ||||||||||||
| 26 | |||||||||||||
| 27 | =cut | ||||||||||||
| 28 | |||||||||||||
| 29 | 1 | ▶ | sub dir_list { | ||||||||||
|
Subroutine
dir_list: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||
| 30 | 1 | my ($dir, $sort_order, $pattern) = @_; | |||||||||||
| 31 | |||||||||||||
| 32 | 1 | ▶ | $pattern //= qr/^[0-9]{1,2}$/; | ||||||||||
|
Condition50%1/2
$pattern //= qr/^[0-9]{1,2}$/
MC/DC0%0/1
$pattern //= qr/^[0-9]{1,2}$/
$pattern
Truth table50%1/2
A
A= $pattern
| |||||||||||||
| 33 | 1 | ▶ | $sort_order //= 'high_to_low'; | ||||||||||
|
Condition50%1/2
$sort_order //= "high_to_low"
MC/DC0%0/1
$sort_order //= "high_to_low"
$sort_order
Truth table50%1/2
A
A= $sort_order
| |||||||||||||
| 34 | |||||||||||||
| 35 | 1 | ▶ | opendir my $list_dir, $dir | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | or croak "Couldn't open $dir: $!"; | ||||||||||||
| 37 | |||||||||||||
| 38 | my @files = sort $sort_order | ||||||||||||
| 39 | 1 | grep { m/$pattern/ } | |||||||||||
| 40 | readdir $list_dir; | ||||||||||||
| 41 | |||||||||||||
| 42 | 1 | closedir $list_dir; | |||||||||||
| 43 | |||||||||||||
| 44 | 1 | return @files; | |||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | # Various named sorts for dir_list: | ||||||||||||
| 48 | 0 | ▶ | sub alpha { $a cmp $b; } # alphabetical | ||||||||||
|
Subroutine
alpha: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 49 | 0 | ▶ | sub high_to_low { $b <=> $a; } # numeric, high to low | ||||||||||
|
Subroutine
high_to_low: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 50 | 0 | ▶ | sub low_to_high { $a <=> $b; } # numberic, low to high | ||||||||||
|
Subroutine
low_to_high: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 51 | 0 | ▶ | sub reverse_alpha { $b cmp $a; } # alphabetical, reversed | ||||||||||
|
Subroutine
reverse_alpha: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 52 | |||||||||||||
| 53 | =item file_put_contents($file, $contents) | ||||||||||||
| 54 | |||||||||||||
| 55 | Write $contents string to $file path. Because: | ||||||||||||
| 56 | |||||||||||||
| 57 | L<https://secure.php.net/manual/en/function.file-put-contents.php> | ||||||||||||
| 58 | |||||||||||||
| 59 | =cut | ||||||||||||
| 60 | |||||||||||||
| 61 | 0 | ▶ | sub file_put_contents { | ||||||||||
|
Subroutine
file_put_contents: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 62 | 0 | my ($file, $contents) = @_; | |||||||||||
| 63 | 0 | ▶ | open(my $fh, '>', $file) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | or croak "Unable to open $file for writing: $!"; | ||||||||||||
| 65 | 0 | print $fh $contents; | |||||||||||
| 66 | 0 | close $fh; | |||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | =item file_get_contents($file) | ||||||||||||
| 70 | |||||||||||||
| 71 | Get contents string of $file path. Because: | ||||||||||||
| 72 | |||||||||||||
| 73 | L<https://secure.php.net/manual/en/function.file-get-contents.php> | ||||||||||||
| 74 | |||||||||||||
| 75 | =cut | ||||||||||||
| 76 | |||||||||||||
| 77 | 2 | ▶ | sub file_get_contents { | ||||||||||
|
Subroutine
file_get_contents: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||
| 78 | 2 | my ($file) = @_; | |||||||||||
| 79 | |||||||||||||
| 80 | # Make warnings here fatal, and return some useful info about which file is | ||||||||||||
| 81 | # being opened: | ||||||||||||
| 82 | 0 | ▶ | local $SIG{__WARN__} = sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 83 | 0 | croak "$_[0] when opening $file\n"; | |||||||||||
| 84 | 2 | }; | |||||||||||
| 85 | |||||||||||||
| 86 | 2 | ▶ | open my $fh, '<', $file | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | or croak "Couldn't open $file: $!\n"; | ||||||||||||
| 88 | |||||||||||||
| 89 | 2 | my $contents; | |||||||||||
| 90 | { | ||||||||||||
| 91 | # line separator: | ||||||||||||
| 92 | 2 | local $/ = undef; | |||||||||||
| 93 | 2 | $contents = <$fh>; | |||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | 2 | ▶ | close $fh or croak "Couldn't close $file: $!"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 97 | |||||||||||||
| 98 | 2 | return $contents; | |||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | =back | ||||||||||||
| 102 | |||||||||||||
| 103 | 1; | ||||||||||||