无法 运行 来自 PHP 的 perl 脚本

Cannot run perl script from PHP

我制作了一个 perl 脚本,我想 运行 来自 PHP。

当我做一个正常的例子时

PHP:

exec("perl test.pl",$output);
echo '<pre>'; print_r(array_values($output)); echo '</pre>';

PERL:

#!/usr/bin/perl -s
print "Hiii. This is perl executing in PHP";

然后打印出来:Hiii。这是在 PHP 中 PHP

中执行的 perl

但是当我添加其他 Perl 脚本 (test2) 时:

#!/usr/bin/perl -s

# Function definition
use test_sub qw( :all ) ;

use strict;
use warnings;

if ($ARGV[0] eq "te") 
{
  printf("te chosen to(%d)\n",$ARGV[1]);
  te($ARGV[1]);
}   

而新的 PHP 看起来像:

exec("perl test2.pl",$output);
echo '<pre>'; print_r(array_values($output)); echo '</pre>';

我知道至少应该有一个警告,尽管我没有使用任何参数,但 $output 中似乎没有任何内容。

即使参数在 PHP:

exec("perl test2.pl te 1",$output);
echo '<pre>'; print_r(array_values($output)); echo '</pre>';

没有出现。 我试图查看该文件是否具有函数

的可执行文件
is_executable('test2.pl')

它是什么。

这个 运行s 在 Raspberry PI 2 with Arch 上,我不知道这是否有任何影响?

引用的另一个 perl 文件是:

package test_sub; 

use strict;
use warnings;

use Exporter qw(import);
use Time::HiRes qw(usleep);

our @EXPORT_OK = qw(te);
our %EXPORT_TAGS = (all => \@EXPORT_OK );

sub te {
  my @var = @_;
  printf("settingup te for %d",$var);
}

我已经在终端上自行检查过,在这里它可以正常工作。但是我无法让它通过 PHP.

工作

如有任何帮助,我们将不胜感激。

更新 1

我发现如果我添加以下行:

use test_sub qw( :all ) ;

对于工作的 test.pl 脚本,它也停止提供输出。

这里有两个问题:

第一个:

线下

printf("settingup te for %d",$var);

应该改为

printf("settingup te for %d",@var);

没有初始化为打印的 $var 变量,它是您在子例程中使用的 @var 数组。

第二个:

您应该知道如何编写简单的 php 脚本。

#!/usr/bin/php

<?php
exec("perl test.pl te 1",$output);
echo '<pre>'; print_r(array_values($output)); echo '</pre>';
?>

这对我来说很好,输出:

<pre>Array
(
    [0] => te chosen to(1)
    [1] => settingup te for 1
)
</pre>

perl 脚本:

#!/usr/bin/perl -s

# Function definition
use test_sub qw( :all ) ;

use strict;
use warnings;

if ($ARGV[0] eq "te") 
{
  printf("te chosen to(%d)\n",$ARGV[1]);
  te($ARGV[1]);
}   

perl 模块:

package test_sub; 

use strict;
use warnings;

use Exporter qw(import);
use Time::HiRes qw(usleep);

our @EXPORT_OK = qw(te);
our %EXPORT_TAGS = (all => \@EXPORT_OK );

sub te {
  my @var = @_;
  printf("settingup te for %d",@var);
}

php代码:

#!/usr/bin/php

<?php
exec("perl test.pl te 1",$output);
echo '<pre>'; print_r(array_values($output)); echo '</pre>';
?>

这对你应该也适用。