Subject: | Coercions fail if the object is created during BEGIN |
It appears that the patch to make the coercions polymorphic caused other problems:
Operation `""': no method found, argument in overloaded package Problem at t/begin-coercion.t line 24.
If an class does its own "use overload" as well as using the Class::Std coercion attributes and the only instance of the object is created during BEGIN time then the coercions won't work. This appears to be result of the way the overload module works. The coercions from Class::Std don't get properly registered in the %OVERLOAD hash (I'm guessing it is because the code to do the overloads is eval'd in the CHECK phase). A bless of the class will cause the %OVERLOAD to be properly populated, but in this case since the only bless happened at BEGIN before the coercions were in place %OVERLOAD isn't updated.
I've come up with one fix for this: after the coercions for a package have been put in place in the CHECK block in Class::Std simply do "bless {}, $package;". It works, but just seems wrong. Maybe overload provides a better way of forcing the %OVERLOAD hash to be updated?
Andrew Parker
use Test::More 'no_plan';
package Problem;
use Class::Std;
# overload seems to interfere as well (remove this and it works)
use overload '+' => sub {};
sub as_string : STRINGIFY { return 'string'; }
package main;
our $obj;
BEGIN {
$obj = Problem->new();
}
# use of stringification is what shows the problem
# if another Problem object is created later thowgh, this
# stringification will work
# Uncomment next line and this will work
#Problem->new();
ok("$obj");