1.File::Basename 确定文件或目录基本名(basename)
#!/usr/bin/perl -w
use File::Basename;
my $name="/home/user1/module/File_module/file_module.pl";
my $basename=basename $name;
print "$basename\n";
执行结果:
file_module.pl
如果
my $name="/home/user1/module/File_module";
执行结果:
File_module
2.File::Basename::dirname 显示目录结构
#!/usr/bin/perl -w
use File::Basename;
my $name="/home/user1/module/File_module/file_module.pl";
my $basename=File::Basename::dirname $name;
print "$basename\n";
my $basename=dirname $name; == my $basename=File::Basename::dirname $name;
结果:
/home/user1/module/File_module
3.
#!/usr/bin/perl -w
use File::Basename;
use File::Spec;
my $old_name="/home/user1/module/File_module/file_module.pl";
my $dirname=dirname $old_name;
my $basename=basename $old_name;
#$basename=~s/^/not/;
my $newname=File::Spec->catfile($dirname,$basename);
print "$newname\n";
结果:
/home/user1/module/File_module/file_module.pl
4.File::Find 找文件等于basename.pl 目录文件路径
#!/usr/bin/perl
use strict;
use File::Find;
sub wanted {
if ($_ eq "basename.pl") {
print "$File::Find::name\n";
}
}
find \&wanted,"/home/module";
结果:
/home/user/module/File_module/basename.pl
#!/usr/bin/perl -w
use File::Find;
sub wanted {
if (-f $File::Find::name) {
if ($File::Find::name=~/\.txt/i) {
print "Removing $File::Find::name\n";
unlink $File::Find::name;
}
}
}
find (\&wanted,'/home/user/module');
阅读(666) | 评论(0) | 转发(0) |