2012-06-04 42 views
0

我試圖讓mod_perl在我的apache安裝上工作以便使用perlhandler。通過軟件包「Apache2 :: RequestRec」找不到對象方法「***」

我第一次在我的域名的子目錄中與此虛擬主機

<VirtualHost *:80> 
    ServerAdmin [email protected] 
    ServerName ***.fr.cr 

    DocumentRoot /var/www/aw 
    <Directory /var/www/aw/> 
      AllowOverride None 
      Order allow,deny 
      allow from all 
    </Directory> 

    PerlModule test2::Rules2 
    alias /perl2/ /usr/lib/perl5/test2/ 
    <Location /perl2/> 
      Order allow,deny 
      allow from all 
      SetHandler perl-script 
      PerlHandler test2::Rules2 
    </Location> 

    ErrorLog ${APACHE_LOG_DIR}/aw.error.log 

    # Possible values include: debug, info, notice, warn, error, crit, 
    # alert, emerg. 
    LogLevel warn 

    CustomLog ${APACHE_LOG_DIR}/aw.access.log combined 
</VirtualHost> 

這裏tryied,它工作正常,當我去* .fr.cr/perl2/

但,當我嘗試directoly做給我的域的根,這個虛擬主機:

<VirtualHost *:80> 
    ServerAdmin [email protected] 
    ServerName ***.fr.cr 

    DocumentRoot /var/www/aw 
    <Directory /var/www/aw/> 
      AllowOverride None 
      Order allow,deny 
      allow from all 
    </Directory> 

    PerlModule aw::main 
    alias//usr/lib/perl5/aw/ 
    <Location /> 
      Order allow,deny 
      allow from all 
      SetHandler perl-script 
      PerlHandler aw::main 
    </Location> 

    ErrorLog ${APACHE_LOG_DIR}/aw.error.log 

    # Possible values include: debug, info, notice, warn, error, crit, 
    # alert, emerg. 
    LogLevel warn 

    CustomLog ${APACHE_LOG_DIR}/aw.access.log combined 
</VirtualHost> 

我得到錯誤500,以及Apache日誌有這樣一行:

Can't locate object method "content_type" via package "Apache2::RequestRec" at /usr/lib/perl5/aw/main.pm line 6.\n 

爲i 2碼

一個缺少的「打印」包和一個缺少「CONTENT_TYPE」包,和第一個具有「CONTENT_TYPE」,但誤差測試奇怪的後來在代碼中。

我想我錯過了我的虛擬主機,因爲它在一種情況下工作,而不是在其他相同的代碼。

謝謝!

編輯:代碼: 不工作:

package aw::main; 
use Apache2::Const qw(:common); 

sub handler { 
    my $r = shift; 
    $r->content_type("text/plain"); 
    $r->print("mod_perl rules!\n"); 
    return OK; 
} 
1; 

和工作:

package test2::Rules2; 
use Apache2::Const qw(:common); 

sub handler { 
my $r = shift; 
$r->content_type("text/plain"); 
$r->print("mod_perl rules!\n"); 
return OK; 
} 
1; 
+0

最可能的結論是,aw :: main中存在一個bug,它不在test2 :: Rules2中。 – ikegami

+0

我複製/粘貼代碼。我會嘗試複製文件並重命名它 –

+0

然後我是對的。對於初學者來說,你的'package'聲明是錯誤的。 – ikegami

回答

0

我剛剛一直在努力與同樣的問題,我想我已經找到了答案:

當您在Perl中調用對象方法時,例如myinstance-> themethod(arg1)然後方法獲取類(package)的名稱作爲第一個參數,第一個參數作爲第二個參數。如果您以靜態方法調用方法,例如class :: method(arg1),那麼子例程獲取的第一個參數是第一個參數。就像這樣:

#!/usr/bin/perl 

print "Calling as an object method:\n"; 
fish->chips('lettuce'); 
print "Calling as a static method:\n"; 
fish::chips('lettuce'); 

{package fish; 

sub chips { 
    my $x=shift; 
    my $y=shift; 

    print "\$x is $x and \$y is $y\n"; 
    } 
} 

和輸出是:

Calling as an object method: 
$x is fish and $y is lettuce 
Calling as a static method: 
$x is lettuce and $y is 

mod_perl的呼喚你的處理程序作爲一個對象的方法。它將包名稱作爲第一個參數。如果添加shift並丟棄第一個參數shift第二個參數,那麼您的$r可能就是您要查找的請求對象。

再次查看我的Apache conf文件後,我意識到我已經給予處理程序指令PerlResponseHandler Fish->chips而不是PerlResponseHandler Fish::chips。在找到我想要使用的處理程序時,我在mod_perl方面遇到了一些麻煩。當我指定Fish並命名處理程序sub handler {...時,mod_perl找不到它。同樣,當我指定處理程序名稱時,如Fish::handler(或者我重命名它)Fish::chips那麼Apache將在名爲Fish的目錄中查找名爲chips.pm的文件。

我不知道這是否是mod_perl解析或解析處理程序名稱的實際錯誤,但至少是非常脆弱的行爲。除非我在這裏錯過了一些東西,如果我是我希望有人能指出它。

我希望有幫助。

相關問題