3

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]); }
clt60
  • 62,119
  • 17
  • 107
  • 194

1 Answers1

6
&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.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • The dup contains only `goto &name`. (not counting the subref and `*`). This is simple and clear answer. – clt60 Dec 02 '15 at 17:07
  • 1
    The "duplicate" answers your question in the question itself. I think this makes it a very poor choice of duplicate. I've reopened this question since this one is far clearer than the other one and fare more likely to be of use to others. /// `goto &name` is like `&name`, but it removes the stack frame from the stack. You might want to do that to it from `croak` (usually a bad thing) or to save memory, but it's a little slower. – ikegami Dec 02 '15 at 17:08