Extract file extension: Difference between revisions
Content deleted Content added
→{{header|Perl 6}}: restrict to ASCII letters and numbers |
→{{header|Perl}}: shorter, cleaner code |
||
Line 764: | Line 764: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<lang Perl>#!/usr/bin/perl |
|||
use strict ; |
|||
use warnings ; |
|||
{{trans|Perl 6}} |
|||
sub extension { |
<lang perl>sub extension { |
||
my $filename = shift ; |
|||
my $path = shift; |
|||
$path =~ / \. [a-z0-9]+ $ /xi; |
|||
$& // ''; |
|||
⚫ | |||
else { |
|||
my @parts = split ( /\./ , $filename ) ; |
|||
Testing: |
|||
my $extension = $parts[ -1 ] ; |
|||
<lang perl>printf "%-35s %-11s\n", $_, "'".extension($_)."'" |
|||
if ( $extension =~ /[\/_]/ ) { |
|||
for qw[ |
|||
return "" ; |
|||
http://example.com/download.tar.gz |
|||
} |
|||
CharacterModel.3DS |
|||
else { |
|||
.desktop |
|||
return ".$extension" ; |
|||
document |
|||
document.txt_backup |
|||
} |
|||
/etc/pam.d/login |
|||
} |
|||
];</lang> |
|||
map { print ("file name: $_ , extension : " . extension( $_ ) . "\n" ) } |
|||
( "picture.jpg" , "http://mywebsite.com/picture/image.png" , |
|||
"myuniquefile.longextension" , "IAmAFileWithoutExtension" , |
|||
"/path/to.my/file" , "file.odd_one" ) ; |
|||
⚫ | |||
{{out}} |
{{out}} |
||
<pre> |
|||
<pre>file name: picture.jpg , extension : .jpg |
|||
http://example.com/download.tar.gz '.gz' |
|||
CharacterModel.3DS '.3DS' |
|||
file name: myuniquefile.longextension , extension : .longextension |
|||
.desktop '.desktop' |
|||
file name: IAmAFileWithoutExtension , extension : |
|||
document '' |
|||
file name: /path/to.my/file , extension : |
|||
document.txt_backup '' |
|||
file name: file.odd_one , extension : |
|||
/etc/pam.d/login '' |
|||
</pre> |
</pre> |
||