Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to conditionally "use bigint" with Perl?

I know I can conditionally use a module in Perl but what about the "pragmas"? My tests have shown that use bigint can be much slower than normal math in Perl and I only need it to handle 64-bit integers so I only want to use it when Perl wasn't built with 64-bit integer support, which I also know how to check for using the Config module.

I tried various things with eval and BEGIN blocks but couldn't work out a way to conditionally use bigint. I know I can use Math::BigInt but then I can't use a single codepath for both the bigint and 64-bit cases.

like image 397
hippietrail Avatar asked Mar 15 '11 10:03

hippietrail


1 Answers

This actually works just fine:

use Config;
BEGIN {
  if (! $Config{use64bitint}) {
    require bigint;
    bigint->import;
  }
}

The interaction between different compile-times is complicated (maybe I'll come back and try to explain it later) but suffice it to say that since there's no string eval here, the flag that bigint sets will persist through the rest of the file or block that you put that BEGIN block inside.

like image 188
hobbs Avatar answered Nov 16 '22 03:11

hobbs