How to Use Perl Libraries from PHP

How can I use Perl libraries from PHP?

You could also use PHP::Interpreter from CPAN. This allows you to embed a PHP interpreter in Perl and also, more usefully for you, to create a Perl object in PHP:

<?php
$perl = Perl::getInstance();
$fh = $perl->new("IO::File", "<$file");
while($fh->getline()) {
# ...
}
?>

Perl interpreter for PHP

It looks like all it is doing is embedding perl inside the PHP process. You should see a memory increase of a few megabytes plus any data you create in Perl. It should not slow down any code. It is just another library sitting in memory waiting for you to call it. There are two benefits of this solution: you don't have to waste time spawning another process and you don't have to parse the return values from text being printed.

Another solution is to write a Perl daemon and talk to it over a domain socket, pipe, or some other method of IPC.

You might also be interested in the Perl documentation covering embedding perl.

Perl module not found when script executed by php

before exec, add:

$ENV{PERL5LIB} = "$ENV{PERL5LIB}:/home/user/perl5/i686-linux-thread-multi";

Why using use lib wont work (for pavel's comment):

#!/usr/bin/env perl
use strict;
use warnings;
use lib "hate_you";
use Data::Dumper;

print Dumper(\@INC);

print "In sub process....\n";
exec(qq{perl -MData::Dumper -e "print Dumper(\@INC);"});

will print on my machine:

$VAR1 = [
'hate_you',
'/etc/perl',
'/usr/local/lib/perl/5.14.2',
'/usr/local/share/perl/5.14.2',
'/usr/lib/perl5',
'/usr/share/perl5',
'/usr/lib/perl/5.14',
'/usr/share/perl/5.14',
'/usr/local/lib/site_perl',
'.'
];
In sub process....

$VAR1 = '/etc/perl';
$VAR2 = '/usr/local/lib/perl/5.14.2';
$VAR3 = '/usr/local/share/perl/5.14.2';
$VAR4 = '/usr/lib/perl5';
$VAR5 = '/usr/share/perl5';
$VAR6 = '/usr/lib/perl/5.14';
$VAR7 = '/usr/share/perl/5.14';
$VAR8 = '/usr/local/lib/site_perl';
$VAR9 = '.';

perl automatic module loading like in php

Well, you probably want to read up on the following:

  • AUTOLOAD and AutoLoader
  • autouse
  • Class::AutoUse
  • later
  • etc.

None of those are quite the same as PHP's approach though.

However, it really is best practice to list all "normal" dependencies. This makes it easier to build installers / deploy to CPAN etc. There are a bunch of other modules that deal with loading plugins, where you really don't know what to load until runtime.

Is there some difficulty with figuring out your dependencies, or do you just want to avoid a bunch of "use" statements at the top of each file?

PHP ↔ Perl interface or bindings

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Can you include PHP in a Perl file?

If you simply want to include the resulting output (HTML, etc.) of the PHP script into the perl-generated page you can use backticks to call either the PHP script via php-cli, like this:

First, the test.pl script:

[root@www1 cgi-bin]# cat test.pl
#!/usr/bin/perl
print "This is a perl script\n";
my $output = `php test.php`;
print "This is from PHP:\n$output\n";
print "Back to perl...\n";
exit 0;

Next, the test.php script:

[root@www1 cgi-bin]# cat test.php
<?php

echo "PHP generated this";

?>

Here's the output of running "test.pl":

[root@www1 cgi-bin]# perl test.pl
This is a perl script
This is from PHP:
PHP generated this
Back to perl...


Related Topics



Leave a reply



Submit