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

In reply to MooseX::Declare and overload by FunkyMonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.