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

In reply to (tye)Re: overloading functions and .XS by tye
in thread overloading functions and .XS by symŽ

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.