不管使用什麼程式語言,常常會遇到要拆解檔案路徑和名稱的情況,有些有提供api使用,有些要自己手動來完成,Perl 則提供了一個好用模組來快速解決這問題。
範例如下:

#!/usr/bin/perl

use strict;
use warnings;
use File::Basename;

my $fullpath='YourFileFullpath';
my ($name, $path, $suffix) = fileparse($fullpath);
print "name=$name\n";
print "path=$path\n";
print "suffix=$suffix\n";

結果:
# On Unix
$fullpath="/foo/bar/baz";
# returns
$name="baz", $path="/foo/bar/", suffix="";


# On Windows
$fullpath='C:\foo\bar\baz';
# returns
$name="baz", $path='C:\foo\bar\', suffix="";

# get file name, It does NOT always return the file name portion of a path as you might expect. To be safe, if you want the file name portion of a path use fileparse() .
$fullpath="/foo/bar/baz";
my $name = basename($fullpath);
#return
$name="baz"

arrow
arrow
    創作者介紹
    創作者 mark528 的頭像
    mark528

    狐的窩

    mark528 發表在 痞客邦 留言(0) 人氣()