1Dancer2::FileUtils(3) User Contributed Perl DocumentationDancer2::FileUtils(3)
2
3
4
6 Dancer2::FileUtils - File utility helpers
7
9 version 0.400000
10
12 use Dancer2::FileUtils qw/dirname path path_or_empty/;
13
14 # for 'path/to/file'
15 my $dir = dirname($path); # returns 'path/to'
16 my $path = path($path); # returns '/abs/path/to/file'
17 my $path = path_or_empty($path); # returns '' if file doesn't exist
18
19
20 use Dancer2::FileUtils qw/path read_file_content/;
21
22 my $content = read_file_content( path( 'folder', 'folder', 'file' ) );
23 my @content = read_file_content( path( 'folder', 'folder', 'file' ) );
24
25
26 use Dancer2::FileUtils qw/read_glob_content set_file_mode/;
27
28 open my $fh, '<', $file or die "$!\n";
29 set_file_mode($fh);
30 my @content = read_glob_content($fh);
31 my $content = read_glob_content($fh);
32
33
34 use Dancer2::FileUtils qw/open_file/;
35
36 my $fh = open_file('<', $file) or die $message;
37
38
39 use Dancer2::FileUtils 'set_file_mode';
40
41 set_file_mode($fh);
42
44 Dancer2::FileUtils includes a few file related utilities that Dancer2
45 uses internally. Developers may use it instead of writing their own
46 file reading subroutines or using additional modules.
47
49 my $path = path( 'folder', 'folder', 'filename');
50 Provides comfortable path resolution, internally using File::Spec.
51 'path' does not verify paths, it just normalizes the path.
52
53 my $path = path_or_empty('folder, 'folder','filename');
54 Like path, but returns '' if path doesn't exist.
55
56 dirname
57 use Dancer2::FileUtils 'dirname';
58
59 my $dir = dirname($path);
60
61 Exposes File::Basename's dirname, to allow fetching a directory name
62 from a path. On most OS, returns all but last level of file path. See
63 File::Basename for details.
64
65 set_file_mode($fh);
66 use Dancer2::FileUtils 'set_file_mode';
67
68 set_file_mode($fh);
69
70 Applies charset setting from Dancer2's configuration. Defaults to utf-8
71 if no charset setting.
72
73 my $fh = open_file('<', $file) or die $message;
74 use Dancer2::FileUtils 'open_file';
75 my $fh = open_file('<', $file) or die $message;
76
77 Calls open and returns a filehandle. Takes in account the 'charset'
78 setting from Dancer2's configuration to open the file in the proper
79 encoding (or defaults to utf-8 if setting not present).
80
81 my $content = read_file_content($file);
82 use Dancer2::FileUtils 'read_file_content';
83
84 my @content = read_file_content($file);
85 my $content = read_file_content($file);
86
87 Returns either the content of a file (whose filename is the input), or
88 undef if the file could not be opened.
89
90 In array context it returns each line (as defined by $/) as a separate
91 element; in scalar context returns the entire contents of the file.
92
93 my $content = read_glob_content($fh);
94 use Dancer2::FileUtils 'read_glob_content';
95
96 open my $fh, '<', $file or die "$!\n";
97 binmode $fh, ':encoding(utf-8)';
98 my @content = read_glob_content($fh);
99 my $content = read_glob_content($fh);
100
101 Similar to read_file_content, only it accepts a file handle. It is
102 assumed that the appropriate PerlIO layers are applied to the file
103 handle. Returns the content and closes the file handle.
104
105 my $norm_path=normalize_path ($path);
106 my $escaped_filename = escape_filename( $filename );
107 Escapes characters in a filename that may alter a path when
108 concatenated.
109
110 use Dancer2::FileUtils 'escape_filename';
111
112 my $safe = escape_filename( "a/../b.txt" ); # a+2f+2e+2e+2fb+2etxt
113
115 Nothing by default. You can provide a list of subroutines to import.
116
118 Dancer Core Developers
119
121 This software is copyright (c) 2022 by Alexis Sukrieh.
122
123 This is free software; you can redistribute it and/or modify it under
124 the same terms as the Perl 5 programming language system itself.
125
126
127
128perl v5.36.0 2022-07-22 Dancer2::FileUtils(3)