clt60
clt60

Reputation: 63972

How to cheat on prototype checking?

Want write an simple wrapper to some foreign perl module. Simplified example:

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");
}

This works OK and prints result.

But now i meet a module which using prototypes, e.g. the above looks like:

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

When trying to use the My wrapper package - it says:

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

Is possible "cheat" on prototype checking or i must write my wrapper as this?

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

or even

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

Upvotes: 3

Views: 83

Answers (1)

ikegami
ikegami

Reputation: 386541

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

There are other options.

(\&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.

Method calls always ignore prototypes.

Upvotes: 6

Related Questions