问题描述
我通常如下从 PHP 调用 perl 脚本并以这种方式传递变量,它工作正常,但是现在我正在构建一个可重用的组件,我还想在其中对传递的 perl 脚本名称进行变量化这让我有些头疼,所以我想知道是否有人可以指出更好的方法来做到这一点,因为我的方法不起作用...谢谢...
I normally call perl scripts from PHP as below and pass in variables this way, and it works fine, however now I am building a component for re-use where I want to also variablize the perl script name that I am passing in and this is giving me some headaches, so I am wondering if anyone can point out a better way to do this as my way isn't working.. thanks..
没有可变 perl 文件名的工作方式:
the way that works without variablized perl filename:
$file = "/var/www/other_scripts/perl/apps/perlscript.pl $var1 $var2 $var3 $var4";
ob_start();
passthru($file);
$perlreturn = ob_get_contents();
ob_end_clean();
我尝试对似乎对我不起作用的 perl 文件名进行变量化,您可以在上面看到它是如何在初始"中甚至包含 $var(s) 的,我觉得这很奇怪,但这似乎是它工作的唯一方法,我什至不确定如何使用可变的 perl 文件名复制它:
My attempt to variablize the perl filename that doesn't seem to be working for me, you can see in the above how it is including even the $var(s) in the initial " ", which I find odd but this seems to be the only way that it works and I wasn't sure how to even replicate this with a variablized perl filename:
$perlscript_file = "/var/www/other_scripts/perl/apps/" . $perlscript .".pl";
$file = $perlscript_file . $var1 . $var2 .$var3 . $var4;
ob_start();
passthru($file);
$perlreturn = ob_get_contents();
ob_end_clean();
推荐答案
您的方法行不通,因为您将所有参数连接起来而没有空格,从而有效地使它们成为一个参数.
Your way is not working because you are concatenating all the parameters without spaces, effectively making them one parameter.
试试
$perlscript_file = "/var/www/other_scripts/perl/apps/$perlscript.pl $var1 $var2 $var3 $var4";
顺便说一下,如果参数来自外部来源,您必须使用清理它们escapeshellarg()
.$perlscript
也是如此 - 如果它来自外部来源甚至是用户输入,请执行 escapeshellcmd()
就可以了.
By the way, if the parameters are coming from an external source, you MUST sanitize them using escapeshellarg()
. The same goes for $perlscript
- if it comes from an external source or even user input, do a escapeshellcmd()
on it.
这篇关于从 PHP 调用 Perl 脚本并传入变量,同时还使用可变的 perl 脚本名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!