Dear Monks!

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.
#!/usr/bin/perl package Dummy; use strict; use warnings; use overload '==' => \&iseq; sub new { return bless [ $_[1] ], $_[0];} sub iseq { return $_[0]->[0] == $_[1]->[0] ;} package MyTie; use strict; use warnings; use Carp; our $AUTOLOAD; sub TIEARRAY { my $class = shift; return bless [ map { Dummy->new($_) } @_ ], $class; } sub FETCH { $_[0]->[ $_[1] ]; } sub FETCHSIZE { scalar @{$_[0]}; } sub AUTOLOAD { croak "Undefined method $AUTOLOAD called in " . __PACK +AGE__;} package main; use strict; use warnings; use Data::Dumper; use Devel::Peek; tie my @arr, 'MyTie', 1,3,4; for my $i (@arr){ for my $j (@arr){ # uncomment this row and comment out the next one, and it works # if($i->iseq($j)){ if($i == $j){ print $i->[0] . " == " . $j->[0] . "\n"; } else { print $i->[0] . " != " . $j->[0] . "\n"; } } } exit 0;
Curiously, I get the error:
Operation `0+': no method found, argument in overloaded package Dummy +at 5perltie.pl line 42.
So Perl wants to force one of the arguments in ($i == $j) into numeric context. If I add the 0+ overloaded operator:
use overload '==' => \&iseq, '0+' => \# ... sub num { print "Someone wants me (" . $_[0]->[0] . ") in numeric con +text\n"; }
this is what I get:
Someone wants me (1) in numeric context Someone wants me (1) in numeric context 1 == 1 1 != 3 1 != 4 Someone wants me (1) in numeric context Someone wants me (3) in numeric context 3 == 1 3 == 3 3 != 4 Someone wants me (1) in numeric context Someone wants me (4) in numeric context 4 == 1 4 != 3 4 == 4
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?


In reply to Weird interaction of tie and overload by rg0now

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.