Debian Codegraph @

lib/Dancer2/FileUtils.pm

raw
1 package Dancer2::FileUtils;
2 # ABSTRACT: File utility helpers
3 $Dancer2::FileUtils::VERSION = '2.2.2';
4 use strict;
5 use warnings;
6
7 use Path::Tiny ();
8 use Carp;
9
10 use Exporter 'import';
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 my @parts = @_;
19 return Path::Tiny::path(@parts)->stringify;
20 }
21
22 sub path_or_empty {
23 my @parts = @_;
24 my $path = Path::Tiny::path(@parts);
25
26 # return empty if it doesn't exist
27 return $path->exists ? $path->stringify : '';
28 }
29
30 sub dirname { return Path::Tiny::path(@_)->parent->stringify; }
31
32 sub set_file_mode {
33 my $fh = shift;
34 my $charset = 'UTF-8';
35 binmode $fh, ":encoding($charset)";
36 return $fh;
37 }
38
39 sub open_file {
40 my ( $mode, $filename, $charset ) = @_;
41
42 return Path::Tiny::path($filename)->filehandle(
43 $mode, ':encoding(UTF-8)',
44 );
45 }
46
47 sub read_file_content {
48 my ( $file, $charset ) = @_;
49 $file or return;
50 my $fh = open_file( '<', $file, $charset );
51
52 return wantarray
53 ? read_glob_content($fh)
54 : scalar read_glob_content($fh);
55 }
56
57 sub read_glob_content {
58 my $fh = shift;
59
60 my @content = <$fh>;
61 close $fh;
62
63 return wantarray ? @content : join '', @content;
64 }
65
66 sub escape_filename {
67 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 $filename =~ s/([^A-Za-z0-9_\=\-\~])/sprintf("+%02x", ord($1))/ge;
73 return $filename;
74 }
75
76 1;
77
78 __END__
79
80 =pod
81
82 =encoding UTF-8
83
84 =head1 NAME
85
86 Dancer2::FileUtils - File utility helpers
87
88 =head1 VERSION
89
90 version 2.2.2
91
92 =head1 SYNOPSIS
93
94 use Dancer2::FileUtils qw/dirname path path_or_empty/;
95
96 # for 'path/to/file'
97 my $dir = dirname($path); # returns 'path/to'
98 my $path = path($path); # returns '/abs/path/to/file'
99 my $path = path_or_empty($path); # returns '' if file doesn't exist
100
101
102 use Dancer2::FileUtils qw/path read_file_content/;
103
104 my $content = read_file_content( path( 'folder', 'folder', 'file' ) );
105 my @content = read_file_content( path( 'folder', 'folder', 'file' ) );
106
107
108 use Dancer2::FileUtils qw/read_glob_content set_file_mode/;
109
110 open my $fh, '<', $file or die "$!\n";
111 set_file_mode($fh);
112 my @content = read_glob_content($fh);
113 my $content = read_glob_content($fh);
114
115
116 use Dancer2::FileUtils qw/open_file/;
117
118 my $fh = open_file('<', $file) or die $message;
119
120
121 use Dancer2::FileUtils 'set_file_mode';
122
123 set_file_mode($fh);
124
125 =head1 DESCRIPTION
126
127 Dancer2::FileUtils includes a few file related utilities that Dancer2
128 uses internally. Developers may use it instead of writing their own
129 file reading subroutines or using additional modules.
130
131 =head1 FUNCTIONS
132
133 =head2 my $path = path( 'folder', 'folder', 'filename');
134
135 Calls L<Path::Tiny>'s C<< path(...)->stringify >> path parts.
136 Better if you use C<Path::Tiny::path(@parts)> directly instead.
137
138 If you want to normalize the path (for existing path), use:
139
140 Path::Tiny::path(@parts)->realpath->stringify;
141
142 =head2 my $path = path_or_empty('folder, 'folder','filename');
143
144 Like path, but returns '' if path doesn't exist.
145
146 =head2 dirname
147
148 use Dancer2::FileUtils 'dirname';
149 my $dir = dirname($path);
150
151 # Same thing:
152 use Path::Tiny qw< path >;
153 my $dir = path($path)->parent;
154 my $as_string = $dir->stringify;
155
156 Fetches the directory name of a path
157 On most OS, returns all but last level of file path. See
158 L<Path::Tiny> for details.
159
160 =head2 set_file_mode($fh, $charset);
161
162 use Dancer2::FileUtils 'set_file_mode';
163
164 set_file_mode($fh);
165
166 Applies the specified charset. Defaults to utf-8 if no charset is provided.
167 An empty charset disables encoding layers.
168
169 =head2 my $fh = open_file('<', $file, $charset) or die $message;
170
171 use Dancer2::FileUtils 'open_file';
172 my $fh = open_file('<', $file) or die $message;
173
174 Calls open and returns a filehandle. If a charset is provided, opens the file
175 in that encoding; otherwise defaults to utf-8. An empty charset disables
176 encoding layers.
177
178 =head2 my $content = read_file_content($file, $charset);
179
180 use Dancer2::FileUtils 'read_file_content';
181
182 my @content = read_file_content($file);
183 my $content = read_file_content($file);
184
185 Returns either the content of a file (whose filename is the input), or I<undef>
186 if the file could not be opened.
187
188 In array context it returns each line (as defined by $/) as a separate element;
189 in scalar context returns the entire contents of the file.
190
191 =head2 my $content = read_glob_content($fh);
192
193 use Dancer2::FileUtils 'read_glob_content';
194
195 open my $fh, '<', $file or die "$!\n";
196 binmode $fh, ':encoding(utf-8)';
197 my @content = read_glob_content($fh);
198 my $content = read_glob_content($fh);
199
200 Similar to I<read_file_content>, only it accepts a file handle. It is
201 assumed that the appropriate PerlIO layers are applied to the file handle.
202 Returns the content and B<closes the file handle>.
203
204 =head2 my $escaped_filename = escape_filename( $filename );
205
206 Escapes characters in a filename that may alter a path when concatenated.
207
208 use Dancer2::FileUtils 'escape_filename';
209
210 my $safe = escape_filename( "a/../b.txt" ); # a+2f+2e+2e+2fb+2etxt
211
212 =head1 EXPORT
213
214 Nothing by default. You can provide a list of subroutines to import.
215
216 =head1 AUTHOR
217
218 Dancer Core Developers
219
220 =head1 COPYRIGHT AND LICENSE
221
222 This software is copyright (c) 2026 by Alexis Sukrieh.
223
224 This is free software; you can redistribute it and/or modify it under
225 the same terms as the Perl 5 programming language system itself.
226
227 =cut