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.3.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   34 use utf8;
  5         10  
  5         26  
33 5     5   156 use v5.16;
  5         13  
34 5     5   33 use strict;
  5         10  
  5         116  
35 5     5   23 use warnings;
  5         10  
  5         183  
36              
37 5     5   28 use File::Spec;
  5         10  
  5         130  
38 5     5   26 use Exporter;
  5         9  
  5         253  
39              
40 5     5   30 use Encode;
  5         23  
  5         492  
41 5     5   35 use Encode::Locale;
  5         16  
  5         1703  
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         13 while (@todo) {
68 8         18 my $dn = pop @todo;
69 8 50       42 opendir my $d, encode(locale_fs => $dn) or die "Opening $dn: $!";
70 8         744 my @content = map decode(locale_fs => $_) => readdir $d;
71 8         1042 foreach (File::Spec->no_upwards(@content)) {
72 8 100       168 if (-d (my $dir = File::Spec->catdir($dn, $_))) {
73 4         21 push @todo, $dir
74             } else {
75 4         49 push @out, File::Spec->catfile($dn, $_)
76             }
77             }
78 8         125 closedir $d;
79             }
80              
81             @out
82 4         97 }
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 215 map decode(locale_fs => $_),
97             map CORE::glob,
98             map encode(locale_fs => $_),
99             @_
100             }
101              
102             =back
103              
104             =cut
105              
106             1