File Coverage

blib/lib/PFT/Util.pm
Criterion Covered Total %
statement 36 36 100.0
branch 3 4 75.0
condition n/a
subroutine 10 10 100.0
pod 1 2 50.0
total 50 52 96.1


line stmt bran cond sub pod time code
1             # Copyright 2014-2016 - Giovanni Simoni
2             #
3             # This file is part of PFT.
4             #
5             # PFT is free software: you can redistribute it and/or modify it under the
6             # terms of the GNU General Public License as published by the Free
7             # Software Foundation, either version 3 of the License, or (at your
8             # option) any later version.
9             #
10             # PFT is distributed in the hope that it will be useful, but WITHOUT ANY
11             # WARRANTY; without even the implied warranty of MERCHANTABILITY or
12             # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13             # for more details.
14             #
15             # You should have received a copy of the GNU General Public License along
16             # with PFT. If not, see .
17             #
18             package PFT::Util v1.4.0;
19              
20             =encoding utf8
21              
22             =head1 NAME
23              
24             PFT::Util - Utilities
25              
26             =head1 DESCRIPTION
27              
28             This module contains general utility functions.
29              
30             =cut
31              
32 5     5   35 use utf8;
  5         10  
  5         23  
33 5     5   152 use v5.16;
  5         16  
34 5     5   32 use strict;
  5         8  
  5         110  
35 5     5   24 use warnings;
  5         8  
  5         168  
36              
37 5     5   29 use File::Spec;
  5         9  
  5         140  
38 5     5   26 use Exporter;
  5         8  
  5         227  
39              
40 5     5   30 use Encode;
  5         9  
  5         396  
41 5     5   34 use Encode::Locale;
  5         15  
  5         1634  
42              
43             our @EXPORT_OK = qw/
44             list_files
45             /;
46              
47             =over 1
48              
49             =item files
50              
51             List all files under the given directories.
52              
53             list_files 'foo' 'bar'
54              
55             This is definitely off-scope, but some perl modules are really bad.
56             C is a utter crap! And I don't really want to add more
57             external deps for such a stupid thing.
58              
59             Also, this handles encoding according to locale.
60              
61             =cut
62              
63             sub list_files {
64 4     4 0 11 my @todo = @_;
65 4         7 my @out;
66              
67 4         11 while (@todo) {
68 8         18 my $dn = pop @todo;
69 8 50       43 opendir my $d, encode(locale_fs => $dn) or die "Opening $dn: $!";
70 8         752 my @content = map decode(locale_fs => $_) => readdir $d;
71 8         1006 foreach (File::Spec->no_upwards(@content)) {
72 8 100       172 if (-d (my $dir = File::Spec->catdir($dn, $_))) {
73 4         20 push @todo, $dir
74             } else {
75 4         53 push @out, File::Spec->catfile($dn, $_)
76             }
77             }
78 8         128 closedir $d;
79             }
80              
81             @out
82 4         109 }
83              
84             =item locale_glob
85              
86             A unicode-safe C.
87              
88             Uses the encoding specified in locale.
89              
90             This is different from C in that it accepts a list of glob
91             patterns
92              
93             =cut
94              
95             sub locale_glob {
96 34     34 1 142 map decode(locale_fs => $_),
97             map CORE::glob,
98             map encode(locale_fs => $_),
99             @_
100             }
101              
102             =back
103              
104             =cut
105              
106             1