| 1 | package uinteger; | ||
| 2 | use v5.16.0; | ||
| 3 | use warnings; | ||
| 4 | use integer (); # for $integer::hints_bits | ||
| 5 | |||
| 6 | our $VERSION = "1.001"; | ||
| 7 | |||
| 8 | require XSLoader; | ||
| 9 | XSLoader::load(__PACKAGE__); | ||
| 10 | |||
| 11 | sub import { | ||
| 12 | # turning "use integer" off means we use the default versions of | ||
| 13 | # most bitops, which mostly treat values as UVs (unsigned), while | ||
| 14 | # use integer treats then as IVs (signed) | ||
| 15 | $^H &= ~$integer::hint_bits; | ||
| 16 | $^H{uinteger} = 1; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub unimport { | ||
| 20 | $^H{uinteger} = 0; | ||
| 21 | } | ||
| 22 | |||
| 23 | 1; | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | uinteger - like "use integer", but unsigned | ||
| 28 | |||
| 29 | =head1 SYNOPSIS | ||
| 30 | |||
| 31 | use uinteger; | ||
| 32 | print 1 - 2; # print a large number | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | Rewrites add, subtract, multiply and unary negation in C<use uinteger> | ||
| 37 | context to perform the operation as if the number was an unsigned | ||
| 38 | integer (a Perl C<UV>). | ||
| 39 | |||
| 40 | Negative numbers are treated as their 2's complement representation. | ||
| 41 | |||
| 42 | Most bitops already treat their arguments as unsigned outside of C<use | ||
| 43 | integer> so this doesn't need to cover those. | ||
| 44 | |||
| 45 | =head1 AUTHOR | ||
| 46 | |||
| 47 | Tony Cook <tony@develop-help.com> | ||
| 48 | |||
| 49 | =cut |