Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: MooseX::Declare and overload

by Haarg (Chaplain)
on Apr 01, 2010 at 08:44 UTC ( #832251=note: print w/ replies, xml ) Need Help??


in reply to MooseX::Declare and overload

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.


Comment on Re: MooseX::Declare and overload
Select or Download Code
Re^2: MooseX::Declare and overload
by FunkyMonk (Canon) on Apr 01, 2010 at 10:17 UTC
    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 hit the nail on the head.
        class InDeclareString is dirty { use overload '""' => 'to_string'; has val => (is => 'rw'); method to_string { sprintf "(%s)", $self->val } };

        works perfectly.

        Many thanks

Re^2: MooseX::Declare and overload
by ikegami (Pope) on Apr 01, 2010 at 17:04 UTC

    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.

        oh! I thought "namespace::autoclean" was "namespace::clean"! Thanks, that makes a lot more sense.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://832251]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2015-07-06 23:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (85 votes), past polls