1. 目录下文件搜索
#!/usr/bin/perl
&find_fileindir("/usr/local/apache/htdocs");
sub find_fileindir(){
local($dir) = @_;
opendir(DIR,"$dir"|| die "can't open this $dir");
local @files =readdir(DIR);
closedir(DIR);
for $file (@files){
next if($file=~m/\.$/ || $file =~m/\.\.$/);
if ($file =~/\.(html|htm|shtml)$/i){
print "$dir\/$file \n";
}
elsif(-d "$dir/$file"){
find_fileindir("$dir/$file" );
}
}
}
2. C调用perl,或者,http://www.hailongchang.org/index.php/archives/365
要调用特定的 Perl 函数,可使用 call_argv 函数。该函数调用一个指定的函数,包括给该 Perl 函数提供任何参数的能力。
#include <EXTERN.h>
#include <perl.h>
static PerlInterpreter *my_perl;
int main(int argc, char **argv, char **env)
{
char *print_args[] = {"This","is","a","list","of","printable","items","\n",NULL};
my_perl = perl_alloc();
perl_construct(my_perl);
perl_parse(my_perl, NULL, argc, argv, NULL);
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
call_argv("printwrap", G_DISCARD, print_args);
perl_destruct(my_perl);
perl_free(my_perl);
}
本文介绍了一个使用Perl进行目录下文件搜索的脚本示例,并演示了如何从C程序中调用Perl函数的方法。Perl脚本能够递归地搜索指定目录中的HTML文件。C调用Perl部分则通过使用Perl的C API实现了一个简单的函数调用示例。
2173

被折叠的 条评论
为什么被折叠?



