|Syntactic Confectionery Delight|
Weird interaction of tie and overloadby rg0now (Chaplain)
|on Jul 26, 2005 at 15:02 UTC||Need Help??|
rg0now has asked for the
wisdom of the Perl Monks concerning the following question:
I have been wading through both perltie(1) and overload(3), but I can't seem to find why the snippet below does not work as expected (by me).
What I am trying to do here is to abstract away a very complex XSUB interface consisting of an iterator (here repersented by MyTie) over a C++ class (Dummy in the code below) as a tied array. I want to allow the user to iterate over the classes by a simple foreach. And while doing that, the user should really be able to use the overloaded operators of the Dummy class.
Curiously, I get the error:
So Perl wants to force one of the arguments in ($i == $j) into numeric context. If I add the 0+ overloaded operator:
this is what I get:
Apparently, it works by also using fallback => 1 in this case, but it won't work in my case, since my complicated class won't let Perl default comparison operators work.
By the way, this is perl, v5.8.7 built for i486-linux-gnu-thread-multi.
So, my question is, why Perl wants to force the variables (which come from a tied array) into numeric context, before even trying to apply my overloaded operator? And why does it do that only in the first iteration of the inner loop?
I realized that the BUGS section of the overload man page says something about the relation of tie and overlaod, but I simply can not grok the wording. Is this the bug I have been running into? How to fix it?