Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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.

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;
ok 1 - mxd is ok not ok 2 - mxd is ok (Q) # Failed test 'mxd is ok (Q)' # at 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
Replies are listed 'Best First'.
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?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (13)
As of 2016-02-11 20:20 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (379 votes), past polls