是否可以有条件地“使用 bigint"?与 Perl? [英] Is it possible to conditionally "use bigint" with Perl?

查看:15
本文介绍了是否可以有条件地“使用 bigint"?与 Perl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以有条件地使用 Perl 中的模块,但是pragmas"呢?我的测试表明 use bigint 可能比 Perl 中的普通数学要慢得多,我只需要它来处理 64 位整数,所以我只想在 Perl 不是用 64 位构建时使用它-位整数支持,我也知道如何使用 Config 模块进行检查.

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.

我用 evalBEGIN 块尝试了各种方法,但无法找到有条件地使用 bigint 的方法.我知道我可以使用 Math::BigInt 但是我不能对 bigint 和 64 位情况都使用单个代码路径.

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.

推荐答案

这实际上工作得很好:

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

不同编译时间之间的交互很复杂(也许我稍后会回来尝试解释它)但足以说明由于这里没有字符串 eval,bigint 设置的标志将持续到其余部分将 BEGIN 块放入的文件或块.

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.

这篇关于是否可以有条件地“使用 bigint"?与 Perl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆