0

我需要将当前目录的结尾部分放入一个变量中。我可以做一个

use Cwd;
my $dir = getcwd;

获得完整路径:usr\bjm\scPDB_entries\4dpt但我真正需要的是4dpt从路径的其余部分中分离出来的 ' '。

谢谢!

4

2 回答 2

5

文件::基本名称

use File::Basename qw(basename);
print basename($dir), "\n";
于 2013-11-13T16:18:52.480 回答
4

It is best to use File::Spec, especially if the code is required to work across platforms. The documentation for File::Basename says this.

If your concern is just parsing paths it is safer to use File::Spec's splitpath() and splitdir() methods.

This program does what you ask

use strict;
use warnings;

use File::Spec;

my $cwd = File::Spec->rel2abs;
my @path = File::Spec->splitdir($cwd);
my $dir = $path[-1];
print $dir;

It long-winded mainly because of the object-oriented nature of File::Spec. The helper module File::Spec::Functions allows you to make it more concise, by importing the class methods as local subroutines.

use strict;
use warnings;

use File::Spec::Functions qw/ rel2abs splitdir /;

my $dir = (splitdir(rel2abs))[-1];
print $dir;
于 2013-11-13T16:44:35.357 回答