| 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
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 28 | 18 | ▶ | return File::Spec->canonpath($dir) unless $dir =~ m/^~/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 13 | ▶ | return File::Spec->canonpath($dir) if !defined $expanded or $expanded eq $first; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $expanded or $expanded eq $first
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
| |||||||||||||||||||||||||||||
| 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> | ||||||||||||||||||||||||||||