-
整合 PHP 及 Perl
Posted on March 7th, 2007 4 comments同樣是開源的 Perl 及 PHP 很多時會給人們作比較,而實際上它們各自有自己的優勢及存在價值。Perl 在系統管理及字串處理上較有優勢,而且有十分豐富的函式庫 -- CPAN,很多複雜的功能也可以簡單地完成。
而 PHP 的優勢就是在動態網頁及連接資料庫上面,資料庫的支援上,市面上大部份流行的付費及開源資料庫也支援,這也是近年在網路上 PHP 越來越流行的原因。
如果可以在一個專案上用上兩種語言的長處就很方便。在 PHP 程式內可以透過 PHP::Interpreter 呼叫 CPAN 模組以擴展 PHP 應用,例如 IO ports 操作這類工作也可以在 PHP 內完成。現在需要先安裝 PHP::Interpreter:
1. 前往 search.cpan.org/dist/PHP-Interpreter 下載 PHP::Interpreter。
2. 執行以下指令安裝:
make
make install這樣就完成 PHP::Interpreter 的安裝,以下舉出兩個在 PHP 程式內使用 Perl 的簡單例子:
PHP:-
<?php
-
$perl = Perl::getInstance();
-
$perl->eval(q^
-
print "Executing Perl code in PHP\n";
-
^);
-
?>
以上會的第二行會建立 $perl 物件,而第二行會透過 $perl 物件執行 Perl。而以下的 $perl->call() 方法就可以傳入及回傳變數:
PHP:-
<?php
-
$perl = Perl::getInstance();
-
$upper = $perl->call('ucfirst', 'hello');
-
?>
上面的第三行是執行 perl 的 ucfirst 函式,並傳入參 "hello"。而 PHP::Interpreter 也可以讓 Perl 執行 PHP 的能力,詳細可以參閱 PHP::Interpreter 使用手冊。
4 responses to “整合 PHP 及 Perl”
-
[...] 整合 PHP 及 Perl [...]
-
PHP和Perl都有迷人之處~~~
要整合又覺得麻煩,真是煩惱~~~~~ -
請問版主您的php和perl 的版本是多少
我在linux上面安裝 PHP::Interpreter
會錯誤呢 不知是不是版本造成的 -
Dear sir
i install the PHP::Interpreter use the CPAN, and the result is :
CPAN: Storable loaded ok (v2.15)
Going to read /tmp/perl-5.8.8/yes/Metadata
Database was generated on Fri, 14 Sep 2007 06:36:45 GMT
PHP::Interpreter is up to date (1.0.1).cpan[2]>
so, i think the PHP::Interpreter is installed, but i write a php file named kkk.php and the context in this file is
eval( print “Executing Perl code in PHP\n” );
?>
i work in linux, so i run the php kkk.php
the out is :
Fatal error: Class ‘Perl’ not found in /var/www/html/kkk.php on line 3do you meet this case, or not, i do’t know how to fix this problem, if you have any method, please tell me, thank very much
sorry, in the computer i can.t type the chines , and my
english is very pool , holp you can understand my question. 3q
Leave a reply
-
