2015-12-02 85 views
3

想寫一個簡單的包裝到一些外國Perl模塊。簡化示例:如何欺騙原型檢查?

use 5.014; 
use warnings; 

#foreign package 
package Some { 
    sub func { 
     my($x,$y) = @_; 
     return $x.$y; 
    } 
}; 

#my own packages 
package My { 
    #use Some(); 
    sub func { Some::func(@_); } 
} 

package main { 
    #use My; 
    say My::func("res","ult"); 
} 

此工作正常並打印result

但是現在我遇到了一個使用原型的模塊,例如,上面的樣子:

package Some { 
    sub func($$) {  # <-- prototype check 
     my($x,$y) = @_; 
     return $x.$y; 
    } 
}; 

當試圖使用My包裝包 - 它說:

Not enough arguments for Some::func at ppp line 16, near "@_)" 

有可能在原型檢查「欺騙」或者我必須寫我的包裝,因爲這?

sub func { Some::func($_[0],$_[1]); } 

甚至

sub func($$) { Some::func($_[0],$_[1]); } 

回答

7
&Some::func(@_); # Bypass prototype check. 

還有其他的選擇。

(\&Some::func)->(@_); # Call via a reference. 
&Some::func;   # Don't create a new @_. 
goto &Some::func;  # Don't create a new @_, and remove current call frame from stack. 

方法調用總是忽略原型。

+0

該dup只包含'goto&name'。 (不包括子參數和'*')。這是簡單明瞭的答案。 – Nemo

+1

「重複」在問題本身中回答你的問題。我認爲這使得它非常糟糕的重複選擇。我已經重新提出了這個問題,因爲這個問題比另一個問題更爲明確,並且更有可能對其他問題有用。 ///'goto&name'就像'&name'一樣,但它會從堆棧中刪除堆棧幀。你可能想從'croak'(通常是壞事)那樣做,或者爲了節省內存,但速度會慢一些。 – ikegami