| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::WRT::Util; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 11 |  |  | 11 |  | 620 | use strict; | 
|  | 11 |  |  |  |  | 22 |  | 
|  | 11 |  |  |  |  | 317 |  | 
| 4 | 11 |  |  | 11 |  | 56 | use warnings; | 
|  | 11 |  |  |  |  | 17 |  | 
|  | 11 |  |  |  |  | 293 |  | 
| 5 | 11 |  |  | 11 |  | 976 | use open qw(:std :utf8); | 
|  | 11 |  |  |  |  | 2506 |  | 
|  | 11 |  |  |  |  | 129 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 11 |  |  | 11 |  | 1753 | use Carp; | 
|  | 11 |  |  |  |  | 22 |  | 
|  | 11 |  |  |  |  | 616 |  | 
| 8 | 11 |  |  | 11 |  | 623 | use Encode; | 
|  | 11 |  |  |  |  | 10558 |  | 
|  | 11 |  |  |  |  | 919 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 11 |  |  | 11 |  | 72 | use base qw(Exporter); | 
|  | 11 |  |  |  |  | 26 |  | 
|  | 11 |  |  |  |  | 6418 |  | 
| 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 |  |  |  |  |  |  | sub dir_list { | 
| 30 | 1 |  |  | 1 | 1 | 5 | my ($dir, $sort_order, $pattern) = @_; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 1 |  | 33 |  |  | 3 | $pattern    //= qr/^[0-9]{1,2}$/; | 
| 33 | 1 |  | 50 |  |  | 3 | $sort_order //= 'high_to_low'; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 1 | 50 |  |  |  | 51 | opendir my $list_dir, $dir | 
| 36 |  |  |  |  |  |  | or croak "Couldn't open $dir: $!"; | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | my @files = sort $sort_order | 
| 39 | 1 |  |  |  |  | 38 | grep { m/$pattern/ } | 
|  | 7 |  |  |  |  | 45 |  | 
| 40 |  |  |  |  |  |  | readdir $list_dir; | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 1 |  |  |  |  | 19 | closedir $list_dir; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 1 |  |  |  |  | 9 | return @files; | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | # Various named sorts for dir_list: | 
| 48 | 0 |  |  | 0 | 0 | 0 | sub alpha         { $a cmp $b; } # alphabetical | 
| 49 | 0 |  |  | 0 | 0 | 0 | sub high_to_low   { $b <=> $a; } # numeric, high to low | 
| 50 | 0 |  |  | 0 | 0 | 0 | sub low_to_high   { $a <=> $b; } # numberic, low to high | 
| 51 | 0 |  |  | 0 | 0 | 0 | sub reverse_alpha { $b cmp $a; } # alphabetical, reversed | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | =item file_put_contents($file, $contents) | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | Write $contents string to $file path.  Because: | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | L | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | =cut | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | sub file_put_contents { | 
| 62 | 0 |  |  | 0 | 1 | 0 | my ($file, $contents) = @_; | 
| 63 | 0 | 0 |  |  |  | 0 | open(my $fh, '>', $file) | 
| 64 |  |  |  |  |  |  | or croak "Unable to open $file for writing: $!"; | 
| 65 | 0 |  |  |  |  | 0 | print $fh $contents; | 
| 66 | 0 |  |  |  |  | 0 | close $fh; | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | =item file_get_contents($file) | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | Get contents string of $file path.  Because: | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | L | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =cut | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | sub file_get_contents { | 
| 78 | 228 |  |  | 228 | 1 | 882 | my ($file) = @_; | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | # Make warnings here fatal, and return some useful info about which file is | 
| 81 |  |  |  |  |  |  | # being opened: | 
| 82 |  |  |  |  |  |  | local $SIG{__WARN__} = sub { | 
| 83 | 0 |  |  | 0 |  | 0 | croak "$_[0] when opening $file\n"; | 
| 84 | 228 |  |  |  |  | 1669 | }; | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 228 | 50 |  |  |  | 10146 | open my $fh, '<', $file | 
| 87 |  |  |  |  |  |  | or croak "Couldn't open $file: $!\n"; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 228 |  |  |  |  | 758 | my $contents; | 
| 90 |  |  |  |  |  |  | { | 
| 91 |  |  |  |  |  |  | # line separator: | 
| 92 | 228 |  |  |  |  | 372 | local $/ = undef; | 
|  | 228 |  |  |  |  | 1051 |  | 
| 93 | 228 |  |  |  |  | 7166 | $contents = <$fh>; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 | 228 | 50 |  |  |  | 2519 | close $fh or croak "Couldn't close $file: $!"; | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 228 |  |  |  |  | 3021 | return $contents; | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | =back | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | 1; |