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.1;
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   31 use utf8;
  5         11  
  5         24  
33 5     5   154 use v5.16;
  5         13  
34 5     5   36 use strict;
  5         10  
  5         113  
35 5     5   24 use warnings;
  5         8  
  5         168  
36              
37 5     5   27 use File::Spec;
  5         17  
  5         113  
38 5     5   25 use Exporter;
  5         8  
  5         223  
39              
40 5     5   32 use Encode;
  5         9  
  5         375  
41 5     5   30 use Encode::Locale;
  5         17  
  5         1576  
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 10 my @todo = @_;
65 4         8 my @out;
66              
67 4         11 while (@todo) {
68 8         20 my $dn = pop @todo;
69 8 50       40 opendir my $d, encode(locale_fs => $dn) or die "Opening $dn: $!";
70 8         778 my @content = map decode(locale_fs => $_) => readdir $d;
71 8         1003 foreach (File::Spec->no_upwards(@content)) {
72 8 100       169 if (-d (my $dir = File::Spec->catdir($dn, $_))) {
73 4         22 push @todo, $dir
74             } else {
75 4         48 push @out, File::Spec->catfile($dn, $_)
76             }
77             }
78 8         123 closedir $d;
79             }
80              
81             @out
82 4         102 }
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 190 map decode(locale_fs => $_),
97             map CORE::glob,
98             map encode(locale_fs => $_),
99             @_
100             }
101              
102             =back
103              
104             =cut
105              
106             1