| 1 | package App::WRT::FileIO; | ||||||||||
| 2 | |||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 10 | ▶ | use open qw(:std :utf8); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 10 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 10 | ▶ | use Encode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 10 | ▶ | use File::Copy; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 10 | ▶ | use File::Path qw(make_path); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 10 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 10 | ▶ | use App::WRT::Util; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | =pod | ||||||||||
| 15 | |||||||||||
| 16 | =head1 NAME | ||||||||||
| 17 | |||||||||||
| 18 | App::WRT::FileIO - read and write directories and files | ||||||||||
| 19 | |||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||
| 21 | |||||||||||
| 22 | use App::WRT::FileIO; | ||||||||||
| 23 | my $io = App::WRT::FileIO->new(); | ||||||||||
| 24 | |||||||||||
| 25 | =head1 METHODS | ||||||||||
| 26 | |||||||||||
| 27 | =over | ||||||||||
| 28 | |||||||||||
| 29 | =item new($class) | ||||||||||
| 30 | |||||||||||
| 31 | Get a new FileIO object. | ||||||||||
| 32 | |||||||||||
| 33 | =cut | ||||||||||
| 34 | |||||||||||
| 35 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 36 | 2 | my $class = shift; | |||||||||
| 37 | |||||||||||
| 38 | 2 | my %params = ( | |||||||||
| 39 | last_error => '', | ||||||||||
| 40 | ); | ||||||||||
| 41 | |||||||||||
| 42 | 2 | my $self = \%params; | |||||||||
| 43 | 2 | bless $self, $class; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | |||||||||||
| 47 | =item is_dir($path) | ||||||||||
| 48 | |||||||||||
| 49 | =cut | ||||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | sub is_dir { | ||||||||
|
Subroutine
is_dir: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 52 | 0 | return -d $_[0]; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | |||||||||||
| 56 | =item is_regular_file($path) | ||||||||||
| 57 | |||||||||||
| 58 | =cut | ||||||||||
| 59 | |||||||||||
| 60 | 0 | ▶ | sub is_regular_file { | ||||||||
|
Subroutine
is_regular_file: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 61 | 0 | return -f $_[0]; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | |||||||||||
| 65 | =item exists($path) | ||||||||||
| 66 | |||||||||||
| 67 | =cut | ||||||||||
| 68 | |||||||||||
| 69 | 0 | ▶ | sub exists { | ||||||||
|
Subroutine
exists: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 70 | 0 | return -e $_[0]; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | |||||||||||
| 74 | =item dir_list($dir, $sort_order, $pattern) | ||||||||||
| 75 | |||||||||||
| 76 | Return a $sort_order sorted list of files matching regex $pattern in a | ||||||||||
| 77 | directory. | ||||||||||
| 78 | |||||||||||
| 79 | Calls $sort_order, which can be one of: | ||||||||||
| 80 | |||||||||||
| 81 | alpha - alphabetical | ||||||||||
| 82 | reverse_alpha - alphabetical, reversed | ||||||||||
| 83 | high_to_low - numeric, high to low | ||||||||||
| 84 | low_to_high - numeric, low to high | ||||||||||
| 85 | |||||||||||
| 86 | =cut | ||||||||||
| 87 | |||||||||||
| 88 | 1 | ▶ | sub dir_list { | ||||||||
|
Subroutine
dir_list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 89 | 1 | my $self = shift; | |||||||||
| 90 | 1 | return App::WRT::Util::dir_list(@_); | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | # Various named sorts for dir_list: | ||||||||||
| 94 | 0 | ▶ | sub alpha { $a cmp $b; } # alphabetical | ||||||||
|
Subroutine
alpha: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 95 | 0 | ▶ | sub high_to_low { $b <=> $a; } # numeric, high to low | ||||||||
|
Subroutine
high_to_low: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 96 | 0 | ▶ | sub low_to_high { $a <=> $b; } # numberic, low to high | ||||||||
|
Subroutine
low_to_high: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 97 | 0 | ▶ | sub reverse_alpha { $b cmp $a; } # alphabetical, reversed | ||||||||
|
Subroutine
reverse_alpha: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 98 | |||||||||||
| 99 | |||||||||||
| 100 | =item file_put_contents($file, $contents) | ||||||||||
| 101 | |||||||||||
| 102 | Write $contents string to $file path. Because: | ||||||||||
| 103 | |||||||||||
| 104 | L<https://secure.php.net/manual/en/function.file-put-contents.php> | ||||||||||
| 105 | |||||||||||
| 106 | =cut | ||||||||||
| 107 | |||||||||||
| 108 | 0 | ▶ | sub file_put_contents { | ||||||||
|
Subroutine
file_put_contents: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 109 | 0 | my $self = shift; | |||||||||
| 110 | 0 | App::WRT::Util::file_put_contents(@_); | |||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | |||||||||||
| 114 | =item file_get_contents($file) | ||||||||||
| 115 | |||||||||||
| 116 | Get contents string of $file path. Because: | ||||||||||
| 117 | |||||||||||
| 118 | L<https://secure.php.net/manual/en/function.file-get-contents.php> | ||||||||||
| 119 | |||||||||||
| 120 | =cut | ||||||||||
| 121 | |||||||||||
| 122 | 1 | ▶ | sub file_get_contents { | ||||||||
|
Subroutine
file_get_contents: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 123 | 1 | my $self = shift; | |||||||||
| 124 | 1 | return App::WRT::Util::file_get_contents(@_); | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | |||||||||||
| 128 | =item file_copy($source, $dest) | ||||||||||
| 129 | |||||||||||
| 130 | =cut | ||||||||||
| 131 | |||||||||||
| 132 | 0 | ▶ | sub file_copy { | ||||||||
|
Subroutine
file_copy: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 133 | 0 | my ($self, $source, $dest) = @_; | |||||||||
| 134 | 0 | copy($source, $dest); | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | |||||||||||
| 138 | =item dir_make($source, $dest) | ||||||||||
| 139 | |||||||||||
| 140 | =cut | ||||||||||
| 141 | |||||||||||
| 142 | 0 | ▶ | sub dir_make { | ||||||||
|
Subroutine
dir_make: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 143 | 0 | my ($self, $path) = @_; | |||||||||
| 144 | 0 | my $path_err; | |||||||||
| 145 | 0 | make_path($path, { error => \$path_err }); | |||||||||
| 146 | 0 | ▶ | if (@{ $path_err }) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 147 | 0 | $self->{last_error} = Dumper($path_err); | |||||||||
| 148 | 0 | return 0; | |||||||||
| 149 | } | ||||||||||
| 150 | 0 | return 1; | |||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | =back | ||||||||||
| 154 | |||||||||||
| 155 | =cut | ||||||||||
| 156 | |||||||||||
| 157 | 10 | 1; | |||||||||