【发布时间】:2023-03-11 15:43:01
【问题描述】:
我想编写一个 perl 脚本来解析包含大量 64 位整数的文本文件。所有整数都用十六进制写。
我需要
- 从输入中读取十六进制
- 比较 64 位整数(
<、=、>) - 减去 64 位整数
- 输出 64 位十六进制数
我需要使用 32 位 perl,我不能使用任何 CPAN/外部模块(脚本必须是可移植的)。
PS 我的 perl 是 5.8(这是用于脚本的最小版本)
PPS bignum/bigint 错误:
$ perl -e 'use bignum; $_=hex("0x0000123412345678")'
Integer overflow in hexadecimal number at -e line 1.
$ perl -e 'use bigint; $_=hex("0x0000123412345678")'
Integer overflow in hexadecimal number at -e line 1.
PPPS:这里没有from_hex。
$ perl -e 'use Math::BigInt; $_=Math::BigInt->from_hex("0x0000123412345678");'
Can't locate object method "from_hex" via package "Math::BigInt" at -e line 1.
没有qw/hex/:
$ perl -e 'use bigint qw/hex/; $_=hex("0x0000123412345678")'
unknown option hex at /usr/lib/perl5/5.8/bigint.pm line...
PPPPS:但 new() 有效:
$ perl -e 'use Math::BigInt; $_=Math::BigInt->new("0x0000123412345678"); print $_->as_hex(),"\n";'
0x123412345678
【问题讨论】:
-
根据更新中的错误。
hex函数仅在 perl 5.9.4 或更高版本中由 bignum 自动升级。您需要显式导出hex函数(在您的 perl 版本中将是全局函数)或直接使用Math::BigInt->from_hex(...)函数。