blib/lib/Dancer2/FileUtils.pm

filter: Statementstmt 90.4% 38 / 42 Branchbran 90.0% 9 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.3% 12 / 13 Podpod 100% 8 / 8 totaltotal 91.7% 67 / 73 CC 6 SCAR 5.4
CC
6
Cov
90%
read_file_content
3.0
escape_filename
2.0
path_or_empty
2.0
CRAP 1.7
« lib/Dancer2/Core/MIME.pm ↑ Summary lib/Dancer2/Core/Types.pm »
1 package Dancer2::FileUtils;
2 # ABSTRACT: File utility helpers
3 11 $Dancer2::FileUtils::VERSION = '2.3.0';
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 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 1 ▶sub path {
Subroutine path: called CC=1 SCAR=0.0
Pod documented
18 1 my @parts = @_;
19 1 return Path::Tiny::path(@parts)->stringify;
20 }
21  
22 2 ▶sub path_or_empty {
Subroutine path_or_empty: called CC=2 SCAR=6.9
Pod documented
23 2 my @parts = @_;
24 2 my $path = Path::Tiny::path(@parts);
25  
26 # return empty if it doesn't exist
27 2 ▶ return $path->exists ? $path->stringify : '';
Branch: 1 branch
BranchTrueFalseTotal
$path->exists ? : 1 1 2
28 }
29  
30 2 ▶sub dirname { return Path::Tiny::path(@_)->parent->stringify; }
Subroutine dirname: called CC=1 SCAR=0.0
Pod documented
31  
32 0 ▶sub set_file_mode {
Subroutine set_file_mode: not called CC=1 SCAR=6.9
Pod documented
33 0 my $fh = shift;
34 0 my $charset = 'UTF-8';
35 0 binmode $fh, ":encoding($charset)";
36 0 return $fh;
37 }
38  
39 3 ▶sub open_file {
Subroutine open_file: called CC=1 SCAR=0.0
Pod documented
40 3 my ( $mode, $filename, $charset ) = @_;
41  
42 3 return Path::Tiny::path($filename)->filehandle(
43 $mode, ':encoding(UTF-8)',
44 );
45 }
46  
47 3 ▶sub read_file_content {
Subroutine read_file_content: called CC=3 SCAR=11.0
Pod documented
48 3 my ( $file, $charset ) = @_;
49 3 ▶ $file or return;
Branch: 1 branch
BranchTrueFalseTotal
$file or return 2 1 3
50 2 my $fh = open_file( '<', $file, $charset );
51  
52 return wantarray
53 2 ▶ ? read_glob_content($fh)
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
54 : scalar read_glob_content($fh);
55 }
56  
57 2 ▶sub read_glob_content {
Subroutine read_glob_content: called CC=2 SCAR=6.9
Pod documented
58 2 my $fh = shift;
59  
60 2 my @content = <$fh>;
61 2 close $fh;
62  
63 2 ▶ return wantarray ? @content : join '', @content;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
64 }
65  
66 51 ▶sub escape_filename {
Subroutine escape_filename: called CC=2 SCAR=7.0
Pod documented
67 51 ▶ my $filename = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $filename = shift() or return 51 0 51
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 $filename =~ s/([^A-Za-z0-9_\=\-\~])/sprintf("+%02x", ord($1))/ge;
73 51 return $filename;
74 }
75  
76 11 1;
77  
78 __END__
« lib/Dancer2/Core/MIME.pm ↑ Summary lib/Dancer2/Core/Types.pm »