我需要将当前目录的结尾部分放入一个变量中。我可以做一个
use Cwd;
my $dir = getcwd;
获得完整路径:usr\bjm\scPDB_entries\4dpt
但我真正需要的是4dpt
从路径的其余部分中分离出来的 ' '。
谢谢!
use File::Basename qw(basename);
print basename($dir), "\n";
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;