简体   繁体   English

无法从PHP运行Perl脚本

[英]Cannot run perl script from PHP

I have made a perl script which I would like to run from PHP. 我做了一个perl脚本,我想从PHP运行它。

When I do a normal example like 当我做一个正常的例子

PHP: PHP:

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

PERL: PERL:

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

And it prints out: Hiii. 它打印出来:Hiii。 This is perl executing in PHP in PHP 这是在PHP中的PHP中执行的Perl

But when I add my other Perl script (test2): 但是,当我添加其他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]);
}   

And the new PHP looks like: 新的PHP如下所示:

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

I know that there should be a warning atleast eventhough i dont use any arguments, but nothing appears to be in the $output. 我知道即使我不使用任何参数,也应该至少有一个警告,但$ output中似乎没有任何内容。

Even with arguments in PHP: 即使在PHP中带有参数:

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

Nothing appears. 什么也没出现。 I have tried to see if the file in executable with the function 我试图查看该文件是否在具有该功能的可执行文件中

is_executable('test2.pl')

Which it is. 是的

This runs on a Raspberry PI 2 with Arch and I do not know if this could have any impact? 它可以在带有Arch的Raspberry PI 2上运行,我不知道这是否会产生影响?

The other perl file which is referred to is: 引用的另一个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);
}

I have checked in the terminal for it self and here it works as intended. 我已经自行检查了终端,在这里它可以正常工作。 But I cannot get it to work through PHP. 但是我无法通过PHP使用它。

Any help would be much appreciated. 任何帮助将非常感激。

Update 1 更新1

I have found out that if I add the line: 我发现如果我添加以下行:

use test_sub qw( :all ) ;

To the test.pl script which worked it stops giving an output asswell. 对于有效的test.pl脚本,它停止输出输出。

There are two problems here: 这里有两个问题:

First: 第一:

Below line 线下

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

should be changed to 应该更改为

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

There was no $var variable initialized to print, its @var array you are using in your subroutine. 没有初始化要打印的$ var变量,您在子例程中使用的@var数组。

Second: 第二:

You should know how to write a simple php script. 您应该知道如何编写简单的PHP脚本。

#!/usr/bin/php

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

This works fine for me and output: 这对我来说很好,并输出:

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

perl script: 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 module: 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 code: php代码:

#!/usr/bin/php

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

That should work fine for you as well. 那也应该适合您。

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

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