關於 PHP, Linux, Open Source 及個人生活記載的網誌。
RSS icon
  • 整合 PHP 及 Perl

    Posted on March 7th, 2007 Sam Tang 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:
    1. <?php
    2. $perl = Perl::getInstance();
    3. $perl->eval(q^
    4. print "Executing Perl code in PHP\n";
    5. ^);
    6. ?>

    以上會的第二行會建立 $perl 物件,而第二行會透過 $perl 物件執行 Perl。而以下的 $perl->call() 方法就可以傳入及回傳變數:

    PHP:
    1. <?php
    2. $perl = Perl::getInstance();
    3. $upper = $perl->call('ucfirst', 'hello');
    4. ?>

    上面的第三行是執行 perl 的 ucfirst 函式,並傳入參 "hello"。而 PHP::Interpreter 也可以讓 Perl 執行 PHP 的能力,詳細可以參閱 PHP::Interpreter 使用手冊


     

    4 responses to “整合 PHP 及 Perl”

    1. [...] 整合 PHP 及 Perl [...]

    2. PHP和Perl都有迷人之處~~~
      要整合又覺得麻煩,真是煩惱~~~~~

    3. 請問版主您的php和perl 的版本是多少
      我在linux上面安裝 PHP::Interpreter
      會錯誤呢 不知是不是版本造成的

    4. 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 3

      do 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

    *
    To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
    Click to hear an audio file of the anti-spam word