blib/lib/Path/ExpandTilde.pm

filter: Statementstmt 100% 45 / 45 Branchbran 64.2% 9 / 14 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 86.2% 69 / 80 CC 10 SCAR 12.8
CC
10
Cov
88%
expand_tilde
7.1
BEGIN
2.1
BEGIN
2.1
CRAP 3.6
↑ Summary
1 package Path::ExpandTilde;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Glob ':glob';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.003';
11  
12 1 our @ISA = 'Exporter';
13 1 our @EXPORT = 'expand_tilde';
14  
15 1 ▶use constant BSD_GLOB_FLAGS => GLOB_NOCHECK | GLOB_QUOTE | GLOB_TILDE | GLOB_ERR
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /\A(?:MSWin32|VMS|os2|dos|riscos)\z/ ? : 0 1 1
16 # add GLOB_NOCASE as in File::Glob
17 1 | ($^O =~ m/\A(?:MSWin32|VMS|os2|dos|riscos)\z/ ? GLOB_NOCASE : 0);
18  
19 # File::Glob did not try %USERPROFILE% (set in Windows NT derivatives) for ~ before 5.16
20 1 ▶use constant WINDOWS_USERPROFILE => $^O eq 'MSWin32' && $] < 5.016;
Subroutine BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
$^O eq 'MSWin32' && $] < 5.016
!ll&&!rl&&r
100
MC/DC0%0/2
$^O eq 'MSWin32' && $] < 5.016
$^O eq 'MSWin32' $] < 5.016
Truth table33%1/3
A && B
A= $^O eq 'MSWin32'B= $] < 5.016
ABresult
0X0
100
111
21  
22 # File::Glob does not have bsd_glob on 5.6.0, but its glob was the same then
23 1 ▶BEGIN { *bsd_glob = \&File::Glob::glob if $] == 5.006 }
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $] == "5.006" 0 1 1
24  
25 18 ▶sub expand_tilde {
Subroutine expand_tilde: called CC=7 SCAR=19.6
Pod documented
26 18 my ($dir) = @_;
27 18 ▶ return undef unless defined $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 0 18 18
28 18 ▶ return File::Spec->canonpath($dir) unless $dir =~ m/^~/;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir =~ /^~/ 5 13 18
29 # parse path into segments
30 13 my ($volume, $directories, $file) = File::Spec->splitpath($dir, 1);
31 13 my @parts = File::Spec->splitdir($directories);
32 13 my $first = shift @parts;
33 13 ▶ return File::Spec->canonpath($dir) unless defined $first;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first 0 13 13
34 # expand first segment
35 13 my $expanded;
36 13 if (WINDOWS_USERPROFILE and $first eq '~') {
37 $expanded = $ENV{HOME} || $ENV{USERPROFILE};
38 } else {
39 13 (my $pattern = $first) =~ s/([\\*?{[])/\\$1/g;
40 13 ($expanded) = bsd_glob($pattern, BSD_GLOB_FLAGS);
41 13 ▶ croak "Failed to expand $first: $!" if GLOB_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if GLOB_ERROR() 0 13 13
42 }
43 13 ▶ return File::Spec->canonpath($dir) if !defined $expanded or $expanded eq $first;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $expanded or $expanded eq $first 1 12 13
Condition66%2/3
not defined $expanded or $expanded eq $first
l!l&&r!l&&!r
0112
MC/DC50%1/2
not defined $expanded or $expanded eq $first
not defined $expanded $expanded eq $first
Truth table66%2/3
not(A) or B
A= not defined $expandedB= $expanded eq $first
ABresult
1X1
011
000
44 # replace first segment with new path
45 12 ($volume, $directories) = File::Spec->splitpath($expanded, 1);
46 12 $directories = File::Spec->catdir($directories, @parts);
47 12 return File::Spec->catpath($volume, $directories, $file);
48 }
49  
50 1 1;
51  
52 =head1 NAME
53  
54 Path::ExpandTilde - Expand tilde (~) to homedir in file paths
55  
56 =head1 SYNOPSIS
57  
58 use Path::ExpandTilde;
59 my $homedir = expand_tilde('~');
60 my $bashrc = expand_tilde('~/.bashrc');
61 my $pg_home = expand_tilde('~postgres');
62  
63 =head1 DESCRIPTION
64  
65 This module uses C<bsd_glob> from L<File::Glob> to portably expand a leading
66 tilde (C<~>) in a file path into the current or specified user's home
67 directory. No other L<glob metacharacters|File::Glob/"META CHARACTERS"> are
68 expanded.
69  
70 =head1 FUNCTIONS
71  
72 =head2 expand_tilde
73  
74 my $new_path = expand_tilde($path);
75  
76 Exported by default. If the path starts with C<~>, expands that to the current
77 user's home directory. If the path starts with C<< ~I<username> >>, expands
78 that to the specified user's home directory. If the user doesn't exist, no
79 expansion is done. The returned path is canonicalized as by
80 L<File::Spec/"canonpath"> either way.
81  
82 =head1 NOTES
83  
84 The algorithm should be portable to most operating systems supported by Perl,
85 though the home directory may not be found by C<bsd_glob> on some.
86  
87 =head1 BUGS
88  
89 Report any issues on the public bugtracker.
90  
91 =head1 AUTHOR
92  
93 Dan Book <dbook@cpan.org>
94  
95 =head1 COPYRIGHT AND LICENSE
96  
97 This software is Copyright (c) 2018 by Dan Book.
98  
99 This is free software, licensed under:
100  
101 The Artistic License 2.0 (GPL Compatible)
102  
103 =head1 SEE ALSO
104  
105 L<File::Path::Expand>, L<File::HomeDir>, L<File::HomeDir::Tiny>
↑ Summary