Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
Using Perl in Php

Using Perl in Php

Ratings: (0)|Views: 0 |Likes:
Published by aldilnojinjalo

More info:

Published by: aldilnojinjalo on Jun 20, 2013
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as ODT, PDF, TXT or read online from Scribd
See more
See less

11/11/2013

pdf

text

original

 
Using Perl Code from PHP
Intended Audience
This article describes the perl extension for PHP. It is intended for developers who use bothlanguages in their projects, or who are migrating from one language to the other. It could be of general interest to other developers.
Introduction
PHP and perl are two very popular Web programming languages. They both have many librariesand extensions that can simplify the process of development, but often you can find a perl libraryyou want, and not the corresponding library in PHP. (Perl is older then PHP, so naturally it has alarger selection of libraries and extensions.) This was the main reason that the perl extension for PHP was written.Many large projects use both PHP and perl, with some subsystems implemented in PHP, and othersin perl. Often these subsystems need to communicate with each other, and some perl modules - suchas PHP::Include and PHP::Session - have been implemented to achieve this (seehttp://www.cpan.org/modules/by-module/PHP/for more information). However, although theyallow PHP session files to be read, PHP variables to be serialized and simple PHP code to beexecuted from within perl, there is no real communication between the two interpreters.The PHP perl extension was implemented to allow the usage of perl code from within PHP. It is awrapper that embeds the perl interpreter and converts data from PHP to perl and back. At the timeof writing it only provides a one-way interface from PHP to perl, but in the future it could beextended to implement a full two-way interface. The perl extension allows the programmer to dothe following from a PHP script:
load and execute perl files
evaluate perl code
access perl variables
call perl functions
instantiate perl objects
access properties of perl objects
call methods of perl objectsAll these features are accessible through a single API class called
Perl
.PHP's perl extension is available from the PECL web site athttp://pecl.php.net/package/perl. Thelatest development version can be obtained with the following CVS command:
$ cvs -d :pserver:cvs.php.net:/repository co pecl/perl
If you have a full perl installation, the extension will work with it. If you don't have perl on board,
 
you can still communicate with the perl interpreter through PHP by putting a copy of 
 perl58.so
or 
 perl58.dll 
somewhere PHP can find it (in the PHP directory or in your system path).
Perl Interpreter in PHP
To access the perl interpreter from PHP, you must first create an instance of the
Perl
class. Itsconstructor can receive some parameters, but we will omit them at this point. They are necessary for working with perl objects, but not for working with the interpreter itself.
$perl = new Perl();
This line of code creates an instance of the perl interpreter. It is possible to create several instancesof the interpreter, but all of them will use the same one internally, so that all code and variables will be shared across instances. The object
$perl
can be used to execute external perl files, evaluateinline perl code, access perl variables and call perl functions.External perl files can be loaded using the
Perl::require()
method. Take a look at thefollowing example:
Example 1 (test1.pl)
print "Hello from perl! "
Example 1 (test1.php)
<?phpprint "Hello from PHP! "; $perl = new Perl(); $perl->require("test1.pl"); print "Bye! "; ?>
In this example perl outputs a string directly to the PHP output stream, but in some cases you willwant to grab the output as a string and process it with your PHP code. This can be done using thePHP output buffering API:
Example 2 (test2.php)
<?phpob_start();$perl = new Perl(); $perl->require("test1.pl"); $out = ob_get_contents(); ob_end_clean();print "Perl: $out"; ?>
As you can see, it works fine. Of course the same can be done with PHP's system call, but lessefficiently. The
system()
function will start the interpreter each time it's called, whereas
$perl->eval()
uses the embedded interpreter in the same address space and doesn't need to create a new process.As was said earlier, the PHP perl extension can evaluate inline perl code. This method is more
 
useful if you want to execute a small piece of code. With the
Perl::eval()
method you don'tneed to create several small perl files, but can instead simply embed perl code into PHP.
Example 3 (test3.php)
<?phpprint "Hello from PHP! "; $perl = new Perl(); $perl->eval('print "Hello from perl! "'); print "Bye! "; ?>Perl::eval()
accepts only one argument - the perl code to execute, in string format. PHPallows three different ways of writing string literals; single quoted, double quoted or heredoc. Notethat PHP will act on the content of literal strings in the usual way before they are passed to perl (seehttp://www.php.net/manual/language.types.string.php for details).In the previous example we didn't receive any unexpected results from
Perl::eval()
, but wecould do so. The perl interpreter can run the same code in different contexts, and the result can bevery different in those different contexts. For example, a list returned in a scalar context would bereceived as the final element of that list.A
Perl
object uses the scalar context by default, but evaluating in an array or hash context
 requires the use of special tricks. The method
eval()
should not be called directly on the perlinterpreter object, but on the appropriate property (array or hash).
Example 4 (test4.php)
<?php$perl = new Perl(); var_dump($perl->eval('("a","b","c")'));// eval in scalar contextvar_dump($perl->array->eval('("a","b","c")')); // eval in array contextvar_dump($perl->hash->eval('("a","b","c")'));// eval in hash context/* output:string(1) "c"array(3) {[0]=>string(1) "a"[1]=>string(1) "b"[2]=>string(1) "c"}array(2) {["a"]=>string(1) "b"

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->