use*_*896 3 perl multithreading fork process
我发现perl很容易做以下事情:
print "File not found, valid files are:\n\n".`ls DIRECTORY | grep 'php'`;
`rm -rf directory`
my @files_list = split("\n", `ls DIRECTORY | grep 'FILE_NAME_REGEX'`)
Run Code Online (Sandbox Code Playgroud)
做这些事情是不好的做法吗?我觉得这样做要比煞费苦心地实施每件事情要容易得多.我将Perl视为bash的高级版本.
Aln*_*tak 12
使用外部二进制文件是:
在这种特殊情况下,请查看File::Glob模块.
它取决于程序的预期寿命.如果它只是你自己要使用的东西,或者它意味着一次性,那就完全没问题了.这就是它最初的设计目的:"最初设计为UNIX操作系统的粘合语言......"(编程Perl,第2版,第ix页).
另一方面,如果它是一个意图被大量使用,分布广泛,在多个操作系统上运行等的程序,那么最好使用内置输入和CPAN提供的许多软件包.
无论哪种方式,你应该总是使用unlink函数而不是调用rm,grep或map函数而不是调用grep.
现在提出反对意见.TMTWOTDI,正如@ transistor1在评论中所说.而且我也可以调用Perl的whipituptitude的力量.如果cp和mv比更熟悉你File::Copy模块,如果你不担心可移植性,如果你的程序能买得起从启动额外的进程或两个(提示:它可能可以)的性能损失,Perl中可以很容易地将这些工具集成到您的程序中,没有什么 - 没有 - 使用任何可用的工具来尽快完成任务.
而且,有时会有一些一次性的任务,即使你知道如何在Perl中完成工作,Unix工具也是正确的工具.
# I need log.err plus the next two oldest and the next two newest
# files in the current directory. Should I say
chomp(@f = qx[ls -t | grep -C2 log.err]);
# or
@e = sort { -M $a <=> -M $b } glob("*");
($i) = grep { $e[$_] eq 'log.err' } 0..$#e;
@f = @e[$i-2 .. $i+2];
# or
use Acme::OlderNewer::FileFinder;
@f = find_oldernewer_files(".", "log.err", -2, +2);
# ? Or suppose I want a list of all the *.pm files under all
# directories in @INC, and we lucked out so that nothing in @INC
# has any spaces or special characters.
# Is my script any less useful for saying
chomp(@f = `find @INC -name \\*.pm`);
# than
use File::Find;
find( sub { /\.pm$/ && push @f, $File::Find::name }, @INC );
Run Code Online (Sandbox Code Playgroud)