The following code illustrates:
It produces:#!/usr/bin/perl use strict; use warnings; package aaa; { use overload ( '""' => sub { return(1000000000000000) }, '0+' => sub { return(1000000000000000) }, 'fallback' => 1 ); sub new { return (bless({}, shift)); } } package bbb; { use overload ( '""' => sub { return(999999999999999) }, '0+' => sub { return(999999999999999) }, 'fallback' => 1 ); sub new { return (bless({}, shift)); } } package main; { print("Object overloaded as 16-digit integer\n"); my $a = aaa->new(); print("STRING: $a\n"); print('NUMBER: ', 0 + $a, "\n\n"); print("Object overloaded as 15-digit integer\n"); my $b = bbb->new(); print("STRING: $b\n"); print('NUMBER: ', 0 + $b, "\n\n"); print("Addition with 16-digit integer\n"); print('NUMBER: ', 0 + 1000000000000000, "\n"); }
Object overloaded as 16-digit integer STRING: 1000000000000000 NUMBER: 1e+15 Object overloaded as 15-digit integer STRING: 999999999999999 NUMBER: 999999999999999 Addition with 16-digit integer NUMBER: 1000000000000000It's the '1e+15' above that I object to.
Update:Using int(0 + $a) does restore the result back to an integer, but that doesn't address my desire to negate the floating-point conversion in the first place.
In reply to Numeric overloading with 64-bit Perl by jdhedden
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |