File Coverage

blib/lib/ExtUtils/Builder/Util.pm
Criterion Covered Total %
statement 55 85 64.7
branch 9 30 30.0
condition 3 15 20.0
subroutine 15 19 78.9
pod 8 8 100.0
total 90 157 57.3


line stmt bran cond sub pod time code
1             package ExtUtils::Builder::Util;
2             $ExtUtils::Builder::Util::VERSION = '0.020';
3 15     15   343050 use strict;
  15         29  
  15         589  
4 15     15   93 use warnings;
  15         32  
  15         975  
5              
6 15     15   115 use Exporter 5.57 'import';
  15         377  
  15         1161  
7             our @EXPORT_OK = qw/get_perl require_module unix_to_native_path native_to_unix_path command code function split_like_shell/;
8              
9 15     15   123 use Carp 'croak';
  15         98  
  15         902  
10 15     15   90 use Config;
  15         36  
  15         891  
11 15     15   8244 use ExtUtils::Config;
  15         9832  
  15         751  
12 15     15   7582 use ExtUtils::Helpers 0.027 'split_like_shell';
  15         102455  
  15         1167  
13 15     15   131 use File::Spec;
  15         37  
  15         561  
14 15     15   79 use File::Spec::Unix;
  15         30  
  15         428  
15 15     15   77 use Scalar::Util 'tainted';
  15         31  
  15         21330  
16              
17             sub get_perl {
18 5     5 1 190507 my (%opts) = @_;
19 5   33     60 my $config = $opts{config} // ExtUtils::Config->new;
20              
21 5 50 33     197 if (File::Spec->file_name_is_absolute($^X) and not tainted($^X)) {
    0          
22 5         1214362 return $^X;
23             }
24             elsif ($config->get('userelocatableinc')) {
25 0         0 require Devel::FindPerl;
26 0         0 return Devel::FindPerl::find_perl_interpreter($config);
27             }
28             else {
29 0         0 return $config->get('perlpath');
30             }
31             }
32              
33             sub require_module {
34 3     3 1 21 my $module = shift;
35 3         14 (my $filename = "$module.pm") =~ s{::}{/}g;
36 3         1466 require $filename;
37 3         866 return $module;
38             }
39              
40             sub command {
41 0     0 1 0 my (@command) = @_;
42 0         0 require ExtUtils::Builder::Action::Command;
43 0         0 return ExtUtils::Builder::Action::Command->new(command => \@command);
44             }
45              
46             sub code {
47 9     9 1 267778 my %args = @_;
48 9         1048 require ExtUtils::Builder::Action::Code;
49 9         57 return ExtUtils::Builder::Action::Code->new(%args);
50             }
51              
52             sub function {
53 0     0 1 0 my %args = @_;
54 0         0 require ExtUtils::Builder::Action::Function;
55 0         0 return ExtUtils::Builder::Action::Function->new(%args);
56             }
57              
58             my %cache;
59             sub glob_to_regex {
60 1     1 1 2 my $input = shift;
61 1   33     7 return $cache{$input} ||= do {
62 1         5 my $regex = _glob_to_regex_string($input);
63 1         43 qr/^$regex$/;
64             };
65             }
66              
67             sub _glob_to_regex_string {
68 1     1   2 my $glob = shift;
69 1         2 my $in_curlies;
70 1         2 local $_ = $glob;
71              
72 1 50       8 my $regex = !/\A(?=\.)/ ? '(?=[^\.])' : '';
73 1         5 while (!/\G\z/mgc) {
74 3 100 0     23 if (/\G([^\/.()|+^\$@%\\*?{},\[\]]+)/gc) {
    50 0        
    100          
    50          
    50          
    0          
    0          
    0          
    0          
    0          
    0          
75 1         6 $regex .= $1;
76             }
77             elsif (m{\G/}gc) {
78 0 0       0 $regex .= !/\G(?=\.)/gc ? '/(?=[^\.])' : '/'
79             }
80             elsif (/ \G ( [.()|+^\$@%] ) /xmgc) {
81 1         6 $regex .= quotemeta $1;
82             }
83             elsif (/ \G \\ ( [*?{}\\,] ) /xmgc) {
84 0         0 $regex .= quotemeta $1;
85             }
86             elsif (/\G\*/mgc) {
87 1         4 $regex .= "[^/]*";
88             }
89             elsif (/\G\?/mgc) {
90 0         0 $regex .= "[^/]";
91             }
92             elsif (/\G\{/mgc) {
93 0         0 $regex .= "(";
94 0         0 ++$in_curlies;
95             }
96             elsif (/\G \[ ( [^\]]+ ) \] /xgc) {
97 0         0 $regex .= "[\Q$1\E]";
98             }
99             elsif ($in_curlies && /\G\}/mgc) {
100 0         0 $regex .= ")";
101 0         0 --$in_curlies;
102             }
103             elsif ($in_curlies && /\G,/mgc) {
104 0         0 $regex .= "|";
105             }
106             elsif (/\G([},]+)/gc) {
107 0         0 $regex .= $1;
108             }
109             else {
110 0         0 croak sprintf "Couldn't parse at %s|%s", substr($_, 0 , pos), substr $_, pos;
111             }
112             }
113              
114 1         5 return $regex;
115             }
116              
117             sub unix_to_native_path {
118 0     0 1   my ($input) = @_;
119 0           my ($volume, $unix_dir, $file) = File::Spec::Unix->splitpath($input);
120 0           my @splitdir = File::Spec::Unix->splitdir($unix_dir);
121 0           my $catdir = File::Spec->catdir(@splitdir);
122 0           return File::Spec->catpath($volume, $catdir, $file);
123             }
124              
125             sub native_to_unix_path {
126 0     0 1   my ($input) = @_;
127 0           my ($volume, $unix_dir, $file) = File::Spec->splitpath($input);
128 0           my @splitdir = File::Spec->splitdir($unix_dir);
129 0           my $catdir = File::Spec::Unix->catdir(@splitdir);
130 0           return File::Spec::Unix->catpath($volume, $catdir, $file);
131             }
132              
133             1;
134              
135             # ABSTRACT: Utility functions for ExtUtils::Builder
136              
137             __END__