Perlでファイル名の拡張子を取得する
File::Basename モジュールの fileparse 関数を使う。
use strict;
use warnings;
use File::Basename qw/ fileparse /;
my $file = shift @ARGV;
(my $base, my $dir, my $ext) = fileparse($file, qr/\..+$/);
print "dir: $dir\n";
print "base: $base\n";
print "ext: $ext\n";
fileparse の返り値の順番が、ファイル名、ディレクトリ名、拡張子であることに注意。ふつうディレクトリ名が最初だと思うよなぁ。
実行結果:
^o^ > perl fileparse.pl sample.txt
dir: .\
base: sample
ext: .txt
