1

I have made a perl script which I would like to run from PHP.

When I do a normal example like

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";

And it prints out: Hiii. This is perl executing in PHP in PHP

But when I add my other Perl script (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:

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.

Even with arguments in 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?

The other perl file which is referred to is:

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.

Any help would be much appreciated.

Update 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.

1
  • 1
    Why do you have the -s flag enabled in your Perl shebang? Commented Aug 12, 2015 at 10:05

1 Answer 1

2

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.

Second:

You should know how to write a simple php script.

#!/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:

#!/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:

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:

#!/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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.