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).
Curiously, I get the error:#!/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;
So Perl wants to force one of the arguments in ($i == $j) into numeric context. If I add the 0+ overloaded operator:Operation `0+': no method found, argument in overloaded package Dummy +at 5perltie.pl line 42.
this is what I get:use overload '==' => \&iseq, '0+' => \# ... sub num { print "Someone wants me (" . $_[0]->[0] . ") in numeric con +text\n"; }
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.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
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?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Weird interaction of tie and overload
by ikegami (Patriarch) on Jul 26, 2005 at 16:22 UTC | |
by rg0now (Chaplain) on Jul 27, 2005 at 07:27 UTC | |
by ikegami (Patriarch) on Jul 27, 2005 at 14:02 UTC |