symŽ has asked for the wisdom of the Perl Monks concerning the following question:

Trying to make a perl wrapper for C++ ...fun. Before I recode a header file I want to make sure that I can't have more than one Xsub in my .xs file with the same name. C handles this with no problems, don't think perl likes it much though.
Can I have more than one xsub with the same name with different parameters?

Replies are listed 'Best First'.
(tye)Re: overloading functions and .XS
by tye (Sage) on Jun 09, 2001 at 10:56 UTC

    If you wish to multiplex (overload) by number of arguments, I highly recommend having a simple subroutine written in Perl that does the multiplexing.

    You can either have several XS methods named things like method_2, method_3, method_4, or (usually better) have the Perl subroutine fill in defaults when too few arguments are given and just have one (or maybe two) XS routines that expect lots of arguments.

    The less code in the *.xs file and the more in the *.pm file, the happier you and your module users will be. Although it is possible to do lots of fancy argument validation and processing in the *.xs file, it is hard to learn, hard to get right, hard to debug, hard to understand after you've (or someone else has) written it, hard to upgrade later, leads to sub-standard interfaces, and is more likely to break with new versions of Perl.

    Better still, is to make the API better than it was in C++ by using (at least some) named arguments, which is quite easy to do in a Perl subroutine. See createFile() in Win32API::File for an example.

            - tye (but my friends call me "Tye")
Re: overloading functions and .XS
by wog (Curate) on Jun 09, 2001 at 00:23 UTC
    A little experimentation reveals "No." (Though, I would have hoped you had already done that.) However you can use (...), to specify your parameters to one xsub and have that xsub look at how many arguments it has, and maybe their types, and call what should be called. (Of course, see perlxs.) Alternately, of course, you could have multiple xsubs and have a perl function that would call the right one in the same way.

    And, by the way, C does not support overloaded functions. C++ does, however.

Re: overloading functions and .XS
by Anonymous Monk on Jun 09, 2001 at 00:03 UTC
    Of course. How do you think perl himself handles the select-function? If you need to declare two XS with one paramset each or one XS with varying args I don't know. I don't deal in XS but one method works for sure.