我通过 -f 选项获取用户输入,无论他输入什么,相应的文件都会被递归搜索。
我的问题是:当用户输入“tmp *”时,它也会搜索“abctmp”、“xyztmp”等。我想要做的是,只有以 tmp 开头的文件应该出现。简而言之,无论用户输入什么文件,都应该被推送到数组中。
目前我正在这样做,但我确信有一些优雅、简短的方法可以做到这一点。
#! /perl/bin/perl
use strict;
use warnings;
use File::Find;
use getopt::Long;
my $filename="tmp*.txt";
find( { wanted => \&wanted,
preprocess => \&dir_search,
}, '.');
sub wanted{
my $regex;
my $myop;
my @mylist;
my $firstchar= substr($filename, 0,1); # I am checking first character.
# Whether it's ".*tmp" or just "tmp*"
if($filename=~ m/[^a-zA-Z0-9_]/g){ #If contain wildcard
if($firstchar eq "."){ # first character "."
my $myop = substr($filename, 1,1);
my $frag = substr($filename,2);
$filename = $frag;
$regex = '\b(\w' . ${myop}. ${filename}. '\w*)\b';
# Has to find whatever comes before 'tmp', too
} else {
$regex = '\b(' . ${myop}. ${filename}. '\w*)\b';
# Like, "tmp+.txt" Only search for patterns starting with tmp
}
if($_ =~ /$regex/) {
push(@mylist, $_);
}
} else {
if($_ eq $filename) { #If no wildcard, match the exact name only.
push(@mylist, $_);
}
}
}
sub dir_search {
my (@entries) = @_;
if ($File::Find::dir eq './a') {
@entries = grep { ((-d && $_ eq 'g') ||
((-d && $_ eq 'h') ||
(!(-d && $_ eq 'x')))) } @entries;
# Want from 'g' and 'h' folders only, not from 'x' folder
}
return @entries;
}
另一件事是,我只想搜索“.txt”文件。我应该把那个条件放在哪里?