File Coverage

blib/lib/Dancer2/FileUtils.pm
Criterion Covered Total %
statement 35 39 89.7
branch 9 10 90.0
condition n/a
subroutine 12 13 92.3
pod 8 8 100.0
total 64 70 91.4


line stmt bran cond sub pod time code
1             package Dancer2::FileUtils;
2             # ABSTRACT: File utility helpers
3             $Dancer2::FileUtils::VERSION = '2.1.0';
4 11     11   570990 use strict;
  11         26  
  11         479  
5 11     11   74 use warnings;
  11         20  
  11         663  
6              
7 11     11   3821 use Path::Tiny ();
  11         71103  
  11         305  
8 11     11   63 use Carp;
  11         47  
  11         935  
9              
10 11     11   71 use Exporter 'import';
  11         136  
  11         6226  
11             our @EXPORT_OK = qw(
12             dirname open_file path read_file_content read_glob_content
13             path_or_empty set_file_mode escape_filename
14             );
15              
16              
17             sub path {
18 1     1 1 188 my @parts = @_;
19 1         3 return Path::Tiny::path(@parts)->stringify;
20             }
21              
22             sub path_or_empty {
23 2     2 1 4844 my @parts = @_;
24 2         14 my $path = Path::Tiny::path(@parts);
25              
26             # return empty if it doesn't exist
27 2 100       204 return $path->exists ? $path->stringify : '';
28             }
29              
30 2     2 1 4253 sub dirname { return Path::Tiny::path(@_)->parent->stringify; }
31              
32             sub set_file_mode {
33 0     0 1 0 my $fh = shift;
34 0         0 my $charset = 'UTF-8';
35 0         0 binmode $fh, ":encoding($charset)";
36 0         0 return $fh;
37             }
38              
39             sub open_file {
40 3     3 1 343534 my ( $mode, $filename, $charset ) = @_;
41              
42 3         23 return Path::Tiny::path($filename)->filehandle(
43             $mode, ':encoding(UTF-8)',
44             );
45             }
46              
47             sub read_file_content {
48 3     3 1 10927 my ( $file, $charset ) = @_;
49 3 100       27 $file or return;
50 2         25 my $fh = open_file( '<', $file, $charset );
51              
52             return wantarray
53 2 100       575 ? read_glob_content($fh)
54             : scalar read_glob_content($fh);
55             }
56              
57             sub read_glob_content {
58 2     2 1 5 my $fh = shift;
59              
60 2         57 my @content = <$fh>;
61 2         83 close $fh;
62              
63 2 100       24 return wantarray ? @content : join '', @content;
64             }
65              
66             sub escape_filename {
67 51 50   51 1 7460 my $filename = shift or return;
68              
69             # based on escaping used in CHI::Driver. Our use-case is one-way,
70             # so we allow utf8 chars to be escaped, but NEVER do the inverse
71             # operation.
72 51         192 $filename =~ s/([^A-Za-z0-9_\=\-\~])/sprintf("+%02x", ord($1))/ge;
  12         55  
73 51         468 return $filename;
74             }
75              
76             1;
77              
78             __END__