| 1 | package String::FilenameStatic; ## Static functions to manipulate a filename and path. | ||||||||||
| 2 | |||||||||||
| 3 | |||||||||||
| 4 | |||||||||||
| 5 | |||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 3 | ▶ | use vars qw(@ISA @EXPORT %EXPORT_TAGS $VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 3 | our $VERSION='0.03'; | |||||||||
| 12 | |||||||||||
| 13 | |||||||||||
| 14 | 3 | @ISA = qw(Exporter); | |||||||||
| 15 | |||||||||||
| 16 | 3 | %EXPORT_TAGS = ( all => [qw( | |||||||||
| 17 | get_path | ||||||||||
| 18 | get_file | ||||||||||
| 19 | remove_trailing_slash | ||||||||||
| 20 | get_file_extension | ||||||||||
| 21 | get_filename | ||||||||||
| 22 | )] ); | ||||||||||
| 23 | |||||||||||
| 24 | 3 | 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 | 2 | ▶ | sub get_path{ # $string ($string) | ||||||||
|
Subroutine
get_path: called CC=3 SCAR=15.0
Pod
documented
| |||||||||||
| 60 | 2 | my $p=shift; | |||||||||
| 61 | |||||||||||
| 62 | 2 | ▶ | if ($p=~ m/\//){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 2 | $p=~ s/(.*)\/(.*)$/$1/; | |||||||||
| 64 | }else{ | ||||||||||
| 65 | 0 | ▶ | if ($p=~ m/^\.*$/){ # only ".." | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 0 | $p=$p; ## nothing to do | |||||||||
| 67 | }else{ | ||||||||||
| 68 | 0 | $p=''; | |||||||||
| 69 | } | ||||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 2 | 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 | 4 | ▶ | sub get_file{ # $string ($string) | ||||||||
|
Subroutine
get_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 83 | 4 | my $p=shift; | |||||||||
| 84 | |||||||||||
| 85 | |||||||||||
| 86 | 4 | $p=~ s/(.*)\/(.*)$/$2/; | |||||||||
| 87 | |||||||||||
| 88 | 4 | 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 | 2 | ▶ | sub remove_trailing_slash{ # $string ($string) | ||||||||
|
Subroutine
remove_trailing_slash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 98 | 2 | my $p=shift; | |||||||||
| 99 | |||||||||||
| 100 | 2 | $p=~ s/\/$//; | |||||||||
| 101 | |||||||||||
| 102 | 2 | 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 | 1 | ▶ | sub get_file_extension{ # $string ($string) | ||||||||
|
Subroutine
get_file_extension: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 113 | 1 | my $p=shift; | |||||||||
| 114 | |||||||||||
| 115 | 1 | $p=get_file($p); | |||||||||
| 116 | 1 | $p=~ m/\.([^\.]*)$/; | |||||||||
| 117 | 1 | $p=$1; | |||||||||
| 118 | |||||||||||
| 119 | 1 | 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 | 2 | ▶ | sub get_filename{ # $string ($string) | ||||||||
|
Subroutine
get_filename: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 134 | 2 | my $p=shift; | |||||||||
| 135 | |||||||||||
| 136 | 2 | $p=get_file($p); | |||||||||
| 137 | 2 | $p=~ s/([\.]?[^\.]*)\.(.*)$/$1/; | |||||||||
| 138 | |||||||||||
| 139 | |||||||||||
| 140 | 2 | return $p; | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 3 | 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<Exporter> | ||||||||||
| 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 | |||||||||||