Swapnajit said:
However, it has introduced a new one - if I have a construct such as
if ($an_integer == 0) {
...
where $an_integer is not necessarily a long long, when I try to run
perl in debug mode (-d), it seems the comparison is done in a separate
subroutine. I can always press 'n' (next) rather than 's' (step), but
it is not clear to me beforehand what are the native operators (such as
'==' in this case) are replaced by underlying subroutines.
Dear Swapnajit,
You can actually look at the code of the Math::BigInt module with
the command:
perldoc -m Math::BigInt
Near the top of that file, you will see many operators being
overloaded, like:
use overload
'=' => sub { $_[0]->copy(); },
'-' => sub { my $c = $_[0]->copy; $_[2] ?
$c->bneg()->badd($_[1]) :
$c->bsub( $_[1]) },
'+' => sub { $_[0]->copy()->badd($_[1]); }
'+=' => sub { $_[0]->badd($_[1]); },
'-=' => sub { $_[0]->bsub($_[1]); },
'*=' => sub { $_[0]->bmul($_[1]); },
'/=' => sub { scalar $_[0]->bdiv($_[1]); },
'%=' => sub { $_[0]->bmod($_[1]); },
....
Which operators are overloaded (and how) depends on the version of
Math::BigInt you use, so you might see a slightly different version of
what I see.
To see what happens when you use the '==' operator on a
Math::BigInt, try running a little program at the command prompt in
debug in debug mode, like this:
perl -MMath::BigInt -wde "Math::BigInt->new(1e15) == 0"
This program will put you in debug mode, ready to create a large number
(1*10^15) and then compare it to zero.
Pressing "s" right away will put you in the new() method (the method
that creates the BigInt). This step has to happen before the '=='
operator is executed. At this point, press "r" to return from this
subroutine and start executing the '==' operator.
Once you press "r", you might see something like this:
69: '<=>' => sub { $_[2] ?
70: ref($_[0])->bcmp($_[1],$_[0]) :
71: $_[0]->bcmp($_[1])},
(Again, what you see will depend on what version of Math::BigInt you
are using.)
If you see the same output I see, it means that the Math::BigInt
module will use the overloaded '<=>' operator to do the work that you
expect the '==' operator to do, and that is to call $_[0]->bcmp($_[1])
(that is, the Math::BigInt::bcmp() routine).
Of course, you can step into the Math::BigInt::bcmp() routine to see
what it does with the debugger's "s" command.
Can anybody provide a list of such operators?
Well, theoretically, the operators that the Math::BigInt module is
compatible with are the same operators that work on a standard integer.
But if you really want to see a list of these operators, just open up
the Math::BigInt module (with the "perldoc -m Math::BigInt" command)
and look for what is being overloaded (near the top of the file).
You'll see a list of standard mathematical operations, but there
might be a few missing (like '=='). In such cases, those operators are
handled by other operators (like '<=>') that are used to derive the
needed information.
I hope this helps, Swapnajit.
-- Jean-Luc Romano