Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

module with conditional xs code

by navalned (Beadle)
on Mar 18, 2020 at 16:48 UTC ( #11114437=perlquestion: print w/replies, xml ) Need Help??

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

I currently have two separate modules I would like to combine. One requires xs code and is only usable on openbsd. The other is its portable version in pure perl. In the Makefile.PL I check the $^O and bail out if it returns openbsd. I'm not sure how best to combine the two. It seems like there would be some simple way of doing so, but I'm afraid I will fall down a rabbit hole trying various methods. I'm willing to bet there is a module or two with similar problems that I could take a look at. Unfortunantly I don't know which. Any advise is appreciated.


Replies are listed 'Best First'.
Re: module with conditional xs code
by tobyink (Canon) on Mar 18, 2020 at 23:23 UTC

    You could look at match::simple and match::simple::XS.

    The way it works is match::simple::XS provides a function called match. Users load match::simple. When match::simple is first loaded, it attempts to load match::simple::XS and import the match function. If it fails, it does a stringy eval on a pure Perl implementation of match. It exports match to the user.

    End users just do use match::simple; if they have match::simple::XS installed, they get the XS implementation automatically.

      I think this will work for me. It looks like I will still have to have two separate modules, but it will make things easier than they are now. Plus I was introduced to two cool modules to play with.


      Indeed, I did something quite similar for Math::Vector::Real and Math::Vector::Real::XS, but instead of creating the pure Perl methods on demand on the main module, they are just regular functions always defined and the XS module overwrites them with its own versions when loaded.

      The advantage of that approach is that it allows me to implement only a subset of the functions on the XS side.

        Thanks for the input. I'll take a look at these. Sounds like it may be a better approach.

Re: module with conditional xs code
by stevieb (Canon) on Mar 18, 2020 at 17:32 UTC

    Do the Perl and XS code overlap, or does the XS provide extra functionality that the Perl distribution doesn't?

      They don't. But reading your reply gave me an idea. Originally I didn't think it possible to do the openbsd side with just perl, but it may be worth a try. If nothing else, but to make everything simpler. I could probably go the other way also and just use some #ifdef's in the xs code.

        Ask yourself whether the efficiency benefits of running C/XS code are really required or not. Then decide if there's any functionality written in C/XS that can't run in Pure Perl without compromising the quality of the distribution.

        It's always easier to deploy a distribution cross-platform if there's no XS for sure, but there are those times where the XS is definitely required.

        If it's not required, and the benefits won't even be noticed, I leave it out.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11114437]
Approved by 1nickt
Front-paged by 1nickt
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2022-05-16 08:55 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (62 votes). Check out past polls.