in reply to Math::BigInt::GMP - direct access to the gmp integer value

Probably, there's no elegant solution here. Math::BigInt::GMP encapsulates the mpz_t opaquely; is extending that not an option? I've no idea how perl modules should handle data sharing with friends.

Anyway, adding multiple magic is very simple with silly code:

use Math::BigInt only => 'GMP'; use Devel::Peek; $$_ =~ m//g, Dump $_ for Math::BigInt->new(1)->{value};
Without the mg_virtual test your code would return NULL, and probably crash somewhere. That's the sort of thing that gives XS its bad rep. :|

Replies are listed 'Best First'.
Re^2: Math::BigInt::GMP - direct access to the gmp integer value
by syphilis (Archbishop) on Jan 11, 2016 at 23:41 UTC
    Without the mg_virtual test your code would return NULL, and probably crash somewhere

    I haven't yet managed to get that to happen. Perhaps I'm just not trying hard enough.
    (Note to self: investigate what happens if NULL gets returned.)

    Update: Just tested what happens if NULL gets returned, and it's fine - just falls through to the slower overloading via stringification.
    The fun will start when a non-NULL (but incorrect) pointer gets returned.

    Current state of this code is to be found in the Math::GMPz module on github:
    git clone git@github.com:sisyphus/math-gmpz Math-GMPz
    The code in question is the C code (in GMPz.xs) that pertains to the overloading of operations with Math::BigInt objects - which is most of the overload_* subs in GMPz.xs.

    Cheers,
    Rob