blib/lib/File/Path/Collapse.pm

filter: Statementstmt 100% 43 / 43 Branchbran 100% 10 / 10 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 100% 78 / 78 CC 10 SCAR 17.5
CC
10
Cov
100%
CollapsePath
10.0
BEGIN
1.0
BEGIN
1.0
CRAP 5.7
↑ Summary
1  
2 package File::Path::Collapse ;
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 BEGIN
9 2 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Sub::Exporter -setup =>
Subroutine BEGIN: called CC=1 SCAR=0.0
11 {
12 exports => [ qw(CollapsePath) ],
13 groups =>
14 {
15 all => [ qw(CollapsePath) ],
16 }
17 2 };
18
19 2 ▶use vars qw ($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 $VERSION = '0.03';
21 }
22  
23 #-------------------------------------------------------------------------------
24  
25 2 ▶use English qw( -no_match_vars ) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 ▶use Readonly ;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 Readonly my $EMPTY_STRING => q{} ;
29 2 Readonly my $UNIX_SEPARATOR => q{/} ;
30 2 Readonly my $DOT => q{.} ;
31 2 Readonly my $DOT_DOT => q{..} ;
32 2 Readonly my $ARRAY_LAST_ENTRY => -1 ;
33  
34 2 ▶use Carp qw(carp croak confess) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 #-------------------------------------------------------------------------------
37  
38 =head1 NAME
39  
40 File::Path::Collapse - Collapses a path as much as possible
41  
42 =head1 SYNOPSIS
43  
44  
45 =head1 DESCRIPTION
46  
47 This module implements ...
48  
49 =head1 DOCUMENTATION
50  
51 =head1 SUBROUTINES/METHODS
52  
53 =cut
54  
55 #-------------------------------------------------------------------------------
56  
57 sub CollapsePath
58 20 ▶{
Subroutine CollapsePath: called CC=10 SCAR=23.0
Pod documented
59  
60 =head2 CollapsePath($path_to_collapse )
61  
62 Collapses the path by removing '.' and '..' from it. Trailing '/' is also removed.
63  
64 I<Arguments>
65  
66 =over 2
67  
68 =item * $path_to_collapse -
69  
70 =back
71  
72 I<Returns> - the collapsed path or undef if undef was passed as argument.
73  
74 =cut
75  
76 20 my ($path_to_collapse, $path_separator) = @_ ;
77  
78 20 ▶return unless defined $path_to_collapse ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path_to_collapse 1 19 20
79  
80 19 ▶$path_separator = $UNIX_SEPARATOR unless defined $path_separator ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path_separator 18 1 19
81  
82 19 ▶my $from_root = substr($path_to_collapse, 0, 1) eq $path_separator ? $path_separator : $EMPTY_STRING ;
Branch: 1 branch
BranchTrueFalseTotal
substr($path_to_collapse, 0, 1) eq $path_separator ? : 6 13 19
83  
84 19 my @uncollapsed_components = split(/\Q$path_separator/sxm, $path_to_collapse) ;
85 19 my @collapsed_components ;
86  
87 19 for my $component(@uncollapsed_components)
88 {
89 97 ▶ if
Branch: 2 branches
BranchTrueFalseTotal
if ($component eq $DOT_DOT and @collapsed_components and $collapsed_components[$ARRAY_LAST_ENTRY] ne $DOT_DOT) { } 10 87 97
elsif ($component eq $DOT or $component eq $EMPTY_STRING) { } 37 50 87
Condition100%6/6
$component eq $DOT_DOT and @collapsed_components and $collapsed_components[$ARRAY_LAST_ENTRY] ne $DOT_DOT
!ll&&!rl&&r
85210
$component eq $DOT_DOT and @collapsed_components
!ll&&!rl&&r
84112
MC/DC100%3/3
$component eq $DOT_DOT and @collapsed_components and $collapsed_components[$ARRAY_LAST_ENTRY] ne $DOT_DOT
$component eq $DOT_DOT @collapsed_components $collapsed_components[$ARRAY_LAST_ENTRY] ne $DOT_DOT
Truth table100%4/4
A and B and C
A= $component eq $DOT_DOTB= @collapsed_componentsC= $collapsed_components[$ARRAY_LAST_ENTRY] ne $DOT_DOT
ABCresult
0XX0
10X0
1100
1111
90 (
91 $component eq $DOT_DOT
92 && @collapsed_components
93 && $collapsed_components[$ARRAY_LAST_ENTRY] ne $DOT_DOT
94 )
95 {
96 10 pop @collapsed_components ;
97 }
98 ▶ elsif($component eq $DOT || $component eq $EMPTY_STRING )
Condition100%3/3
$component eq $DOT or $component eq $EMPTY_STRING
l!l&&r!l&&!r
102750
MC/DC100%2/2
$component eq $DOT or $component eq $EMPTY_STRING
$component eq $DOT $component eq $EMPTY_STRING
Truth table100%3/3
A or B
A= $component eq $DOTB= $component eq $EMPTY_STRING
ABresult
1X1
011
000
99 {
100 }
101 else
102 {
103 50 push @collapsed_components, $component ;
104 }
105 }
106  
107 19 my $collapsed_path = $from_root . join($path_separator, @collapsed_components) ;
108  
109 19 return($collapsed_path, \@uncollapsed_components, \@collapsed_components) ;
110 }
111  
112 #-------------------------------------------------------------------------------
113  
114 2 1 ;
115  
116 =head1 BUGS AND LIMITATIONS
117  
118 None so far.
119  
120 =head1 AUTHOR
121  
122 Nadim ibn hamouda el Khemir
123 CPAN ID: NH
124 mailto: nadim@cpan.org
125  
126 =head1 LICENSE AND COPYRIGHT
127  
128 This program is free software; you can redistribute
129 it and/or modify it under the same terms as Perl itself.
130  
131 =head1 SUPPORT
132  
133 You can find documentation for this module with the perldoc command.
134  
135 perldoc File::Path::Collapse
136  
137 You can also look for information at:
138  
139 =over 4
140  
141 =item * AnnoCPAN: Annotated CPAN documentation
142  
143 L<http://annocpan.org/dist/File-Path-Collapse>
144  
145 =item * RT: CPAN's request tracker
146  
147 Please report any bugs or feature requests to L <bug-file-path-collapse@rt.cpan.org>.
148  
149 We will be notified, and then you'll automatically be notified of progress on
150 your bug as we make changes.
151  
152 =item * Search CPAN
153  
154 L<http://search.cpan.org/dist/File-Path-Collapse>
155  
156 =back
157  
158 =head1 SEE ALSO
159  
160  
161 =cut
↑ Summary