File Coverage

blib/lib/String/FilenameStatic.pm
Criterion Covered Total %
statement 28 31 90.3
branch 1 4 25.0
condition n/a
subroutine 8 8 100.0
pod 5 5 100.0
total 42 48 87.5


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