Perl pack() and integer overflow -
i want pack various results of expressions integer overflow checking in perl (signed, unsigned, big-endian , little-endian). if try:
$ perl -e 'use warnings; print pack("c", 200)' | hexdump -c
i get:
character in 'c' format wrapped in pack @ -e line 1. 00000000 c8 |.| 00000001
is there way check integer overflow occurred in pack() function? or maybe force function fail on overflow? if check range each type before packing (signed 1,2,4,8 bytes, unsigned 1,2,4,8), code seems ugly.
thanks.
you can turn on "pack" warning category , make fatal. overflow cause exception can trapped. e.g.:
for $val (127, 128) { print "$val -> "; if (eval { use warnings fatal => qw(pack); pack("c", $val); }) { print "no overflow"; } else { print "overflow ($@)"; } print "\n"; }
another possibility use (preferably local-ized) $sig{__warn__}
handler , check in handler if warning happened.
Comments
Post a Comment