简体   繁体   English

如何在html perl脚本上打印File :: Find的结果?

[英]How can I print the result of File::Find on html perl script?

I have to show the results from a search in a website. 我必须显示网站搜索的结果。 I'm receiving a search parameter from an html form. 我正在从html表单接收搜索参数。 When I try this: 当我尝试这个:

#!/usr/bin/perl
use CGI qw(:standard);
use File::Find;
my $search = param('value');
my $result = `print find(sub {print $File::Find::name if ($_ eq $search);
}, '/home');`

print "Content-type:text/html\n\n";

print "<html>";
print "<head></head>";
print "<body";
print "$result";
print "</body>";
print "</html>";

I get the following error: 我收到以下错误:

syntax error at ./search.pl line 8, near "print"
Execution of ./search.pl aborted due to complication errors.

I think it's the assigning of find that's prompting the error. 我认为提示错误的是find的分配。 But I've tried other ways to no avail. 但是我尝试了其他方法都无济于事。

Your problem is back tick. 你的问题是回勾。 Back tick only using for execute the linux commands. 后退标记仅用于执行linux命令。

print find(sub {print $File::Find::name if ($_ eq $search); }, '/home'); This is not a linux command. 这不是linux命令。 This is perl script. 这是perl脚本。

So your script should be as follow 所以你的脚本应该如下

#!/usr/bin/perl
use CGI qw(:standard);
use File::Find;
my $search = param('value');
my $result;
find( sub { $result.=$File::Find::name if ($_ eq $search ); }, "/home");

print "Content-type:text/html\n\n";

print "<html>";
print "<head></head>";
print "<body>";
print "$result\n\n";
print "</body>";
print "</html>";

You want to execute the result in back tick run the perl one liner with -e -M switches. 要执行的反勾运行与Perl的一个班轮结果-e -M开关。 -e to execute the perl command. -e执行perl命令。 -M switch using for include the module in you oneliner. -M开关用于将模块包含在oneliner中。 So which is should be as follow 所以应该如下

#!/usr/bin/perl
use CGI qw(:standard);
my $search = param('value');
my $result = ` perl -MFile::Find -e 'print find(sub {print $File::Find::name if(/^$search\$/);}, "/home" )'`;

print "Content-type:text/html\n\n";

print "<html>";
print "<head></head>";
print "<body>";
print "$result";
print "</body>";
print "</html>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM