FunkyMonk has asked for the wisdom of the Perl Monks concerning the following question:

I was writing some code today using MooseX::Declare and I got sick of writing $foo->to_string, so I thought I'd use the situation to play with overload to overload stringification.

It didn't work. So, I went backwards and tried in standard Perl. That worked fine, and so did Moose when I tried that.

Can anyone see anything wrong with the following test case (only the last test fails)?

(Perl 5.10.1 and MooseX::Declare 0.33)

package InPerl; use strict; use warnings; use overload '""' => \&to_string; sub new { bless { val=>$_[1] }, $_[0] } sub to_string { sprintf "(%s)", $_[0]->{val} } package InMoose; use Moose; use overload '""' => \&to_string; has val => is => 'rw'; sub to_string { sprintf "(%s)", $_[0]->val } use MooseX::Declare; class InDeclare { use overload '""' => \&to_string2; has val => is => 'rw'; sub to_string2 { sprintf "(%s)", $_[0]->val } }; package main; use strict; use warnings; use Test::More; my $p = InPerl->new(44); is $p, "(44)", "perl is ok"; is "$p", "(44)", "perl is ok (Q)"; my $m = InMoose->new(val=>66); is $m, "(66)", "moose is ok"; is "$m", "(66)", "moose is ok (Q)"; my $d = InDeclare->new(val=>666); is $d, "(666)", "mxd is ok)"; # PASS: "(666)" is "$d", "(666)", "mxd is ok (Q)"; # FAIL: "InDeclare=HASH(0x2166ab0)" done_testing;

and the output:

ok 1 - perl is ok ok 2 - perl is ok (Q) ok 3 - moose is ok ok 4 - moose is ok (Q) ok 5 - mxd is ok) not ok 6 - mxd is ok (Q) # Failed test 'mxd is ok (Q)' # at /home/bri/git/W/oops/foo line 55. # got: 'InDeclare=HASH(0x20a1960)' # expected: '(666)' 1..6 # Looks like you failed 1 test of 6.

Unless I state otherwise, all my code runs with strict and warnings

Replies are listed 'Best First'.
Re: MooseX::Declare and overload
by Haarg (Priest) on Apr 01, 2010 at 08:44 UTC

    You can see in the MooseX Declare documentation that it implicitly uses namespace::autoclean. This automatic cleaning of imported subs ends up removing the special subs installed by overload as well. There is a bug filed on namespace::autoclean to fix this issue. Another workaround aside from what ikegami posted would be to use the 'is dirty' modifier to avoid the automatic cleaning.

    Test:
    use MooseX::Declare; class InDeclare { use overload '""' => \&to_string2; has val => is => 'rw'; sub to_string2 { sprintf "(%s)", $_[0]->val } }; class InDeclareDirty is dirty { use overload '""' => \&to_string2; has val => is => 'rw'; sub to_string2 { sprintf "(%s)", $_[0]->val } }; package main; use strict; use warnings; use Test::More; my $d = InDeclare->new(val=>666); is $d, "(666)", "mxd is ok"; # PASS: "(666)" is "$d", "(666)", "mxd is ok (Q)"; # FAIL: "InDeclare=HASH(0x2166ab0)" my $dd = InDeclareDirty->new(val=>6666); is $dd, "(6666)", "mxdd is ok"; is "$dd", "(6666)", "mxdd is ok (Q)"; done_testing;
    Results:
    ok 1 - mxd is ok not ok 2 - mxd is ok (Q) # Failed test 'mxd is ok (Q)' # at test.pl line 24. # got: 'InDeclare=HASH(0x100e42bd0)' # expected: '(666)' ok 3 - mxdd is ok ok 4 - mxdd is ok (Q) 1..4 # Looks like you failed 1 test of 4.
      Awesomer! Thanks Haarg++

      This worked, but only when I declared to_string as a sub instead of a method, but I can live with that. And extra thanks for pointing out the bug in namespace::autoclean. That explained the problem clearly.

        There is still a lot of black magic in MooseX::Declare interfering, but if you use use overload '""' => 'to_string'; instead of referencing it symbolically, it should work if to_string is defined as either a sub or a method. It will also allow subclasses to override the stringification behavior.

      You can see in the MooseX Declare documentation that it implicitly uses namespace::autoclean.

      I noticed, but it says the "use namespace::autoclean;" occurs before the "use overload". Something's very buggy.

      And then there's the question as to why test 5 worked. Very weird.

        namespace::autoclean, unlike namespace::clean, removes all imported functions regardless of if they were imported before or after it is used.

        I don't really know why test 5 worked, but Test::More does have special handling for overloaded objects. I'm assuming it is some odd interaction in there.

Re: MooseX::Declare and overload
by ikegami (Patriarch) on Mar 31, 2010 at 21:44 UTC
    I don't know what's going on, but you can work around it by placing
    { package InDeclare; use overload '""' => \&to_string2; }
    after the class.
      Awesome. Just awesome:-)

      Devel::Declare scares me XXXXless. I don't dare look inside anything to do with it.

      Thanks ikegami++