File Coverage

blib/lib/String/FilenameStatic.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package String::FilenameStatic; ## Static functions to manipulate a filename and path.
2              
3              
4             our $VERSION='0.02';
5              
6              
7 3     3   37730 use strict;
  3         8  
  3         122  
8              
9 3     3   15 use vars qw(@ISA @EXPORT %EXPORT_TAGS $VERSION);
  3         6  
  3         184  
10 3     3   21 use Exporter;
  3         7  
  3         108  
11 3     3   678 use File::Spec::Functions qw( catdir splitdir );
  3         719  
  3         185  
12 3     3   4090 use File::Util qw( SL );
  0            
  0            
13              
14              
15              
16             @ISA = qw(Exporter);
17              
18             %EXPORT_TAGS = ( all => [qw(
19             get_path
20             get_file
21             remove_trailing_slash
22             get_file_extension
23             get_filename
24             )] );
25              
26             Exporter::export_ok_tags('all');
27              
28              
29             # This class provides static functions which can be imported to the namespace of
30             # the current class.
31             #
32             #
33             # SYNOPSIS
34             # ========
35             #
36             # # imports all functions
37             # use String::FilenameStatic ':all';
38             #
39             # # imports only two functions
40             # use String::FilenameStatic qw(get_path get_file);
41             #
42             #
43             # LICENSE
44             # =======
45             # You can redistribute it and/or modify it under the conditions of LGPL.
46             #
47             # AUTHOR
48             # ======
49             # Andreas Hernitscheck ahernit(AT)cpan.org
50              
51              
52              
53              
54              
55              
56             # Extracts the path of a filename.
57             #
58             # print get_path('/etc/webserver/httpd.conf');
59             # # writes: '/etc/webserver'
60             #
61             sub get_path{ # $string ($string)
62             my $p=shift;
63              
64             my @p = splitdir( $p );
65              
66             pop @p;
67            
68             $p = catdir( @p );
69            
70              
71             return $p;
72             }
73              
74              
75              
76             # Extracts the whole filename without the path
77             #
78             # print get_file('/etc/webserver/httpd.conf');
79             # # writes: 'httpd.conf'
80             #
81             sub get_file{ # $string ($string)
82             my $p=shift;
83              
84             my @p = splitdir( $p );
85              
86             my $file = pop @p;
87            
88            
89              
90             return $file;
91             }
92              
93              
94              
95             # Returns the path without a slash on the end.
96             # You can use it more than once, without doing
97             # something wrong to the same string.
98             #
99             sub remove_trailing_slash{ # $string ($string)
100             my $p=shift;
101              
102             my $sl = SL();
103              
104             $p=~ s/\Q$sl\E$//;
105              
106            
107            
108             return $p;
109             }
110              
111              
112              
113             # Extracts the extension of a filename
114             #
115             # print get_file_extension('/etc/webserver/httpd.conf');
116             # # writes: 'conf'
117             #
118             sub get_file_extension{ # $string ($string)
119             my $p=shift;
120              
121             $p=get_file($p);
122             $p=~ m/\.([^\.]*)$/;
123             $p=$1;
124              
125             return $p;
126             }
127              
128              
129              
130              
131             # Extracts the whole filename without the path
132             #
133             # print get_filename('/etc/webserver/httpd.conf');
134             # # writes: 'httpd'
135             #
136             # Yes, it sounds very similar to get_file(), but I had
137             # no better idea to describe it without writing get_file_without_extension.
138             #
139             sub get_filename{ # $string ($string)
140             my $p=shift;
141              
142             $p=get_file($p);
143             $p=~ s/([\.]?[^\.]*)\.(.*)$/$1/;
144              
145              
146             return $p;
147             }
148              
149             1;
150             #################### pod generated by Pod::Autopod - keep this line to make pod updates possible ####################
151              
152             =head1 NAME
153              
154             String::FilenameStatic - Static functions to manipulate a filename and path.
155              
156              
157             =head1 SYNOPSIS
158              
159              
160             # imports all functions
161             use String::FilenameStatic ':all';
162              
163             # imports only two functions
164             use String::FilenameStatic qw(get_path get_file);
165            
166              
167              
168              
169             =head1 DESCRIPTION
170              
171             This class provides static functions which can be imported to the namespace of
172             the current class.
173              
174              
175              
176              
177             =head1 REQUIRES
178              
179             L
180              
181              
182             =head1 METHODS
183              
184             =head2 get_file
185              
186             my $string = get_file($string);
187              
188             Extracts the whole filename without the path
189              
190             print get_file('/etc/webserver/httpd.conf');
191             # writes: 'httpd.conf'
192              
193              
194              
195             =head2 get_file_extension
196              
197             my $string = get_file_extension($string);
198              
199             Extracts the extension of a filename
200              
201             print get_file_extension('/etc/webserver/httpd.conf');
202             # writes: 'conf'
203              
204              
205              
206             =head2 get_filename
207              
208             my $string = get_filename($string);
209              
210             Extracts the whole filename without the path
211              
212             print get_filename('/etc/webserver/httpd.conf');
213             # writes: 'httpd'
214              
215             Yes, it sounds very similar to get_file(), but I had
216             no better idea to describe it without writing get_file_without_extension.
217              
218              
219              
220             =head2 get_path
221              
222             my $string = get_path($string);
223              
224             Extracts the path of a filename.
225              
226             print get_path('/etc/webserver/httpd.conf');
227             # writes: '/etc/webserver'
228              
229              
230              
231             =head2 remove_trailing_slash
232              
233             my $string = remove_trailing_slash($string);
234              
235             Returns the path without a slash on the end.
236             You can use it more than once, without doing
237             something wrong to the same string.
238              
239              
240              
241              
242             =head1 AUTHOR
243              
244             Andreas Hernitscheck ahernit(AT)cpan.org
245              
246              
247             =head1 LICENSE
248              
249             You can redistribute it and/or modify it under the conditions of LGPL.
250              
251              
252              
253             =cut
254