And it appears that the overloading is not causing your _copy() function to be called

Damn!! ... that's the oversight I was ... ummmm ... overlooking. I should've thought to stick a printf() in there as a check. Thanks for picking that up.

Another approach is to simply comment out the 'use overload ...' code - which has no effect on the output of the script at all. Oh, well .... live and learn ....

why doesn't use overload '=' => \&_copy; throw an error?

Good question. Thanks BrowserUK.

Cheers,
Rob
Update: I think, at last, I start to see the light. The overloading of the assignment operator takes its effect only when the value of either $num1 or $num2 is subsequently altered using an overloaded operator. Consider the following:
use warnings; use Devel::Peek; package Grief; use overload '++' => \&_inc, '=' => \&_copy, ; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'EOC'; typedef struct { long num; } Number; SV * new(SV * x) { Number * num_obj; SV * obj_ref, * obj; New(1, num_obj, 1, Number); if(num_obj == NULL) croak("Failed to allocate memory in new() function"); obj_ref = newSViv(0); obj = newSVrv(obj_ref, "Grief"); num_obj->num = SvUV(x); sv_setiv(obj, (IV)num_obj); SvREADONLY_on(obj); return obj_ref; } SV * _get_val(SV * obj) { return newSVuv(((Number*)SvIV(SvRV(obj)))->num); } void _inc(SV * obj, SV * second, SV * third) { (((Number*)SvIV(SvRV(obj)))->num)++; } SV * _copy(SV * arg_obj, SV * second, SV * third) { Number * num_obj; SV * obj_ref, * obj; printf( "_copy:%p %p %p\n", arg_obj, second, third ); New(1, num_obj, 1, Number); if(num_obj == NULL) croak("Failed to allocate memory in _copy() function"); obj_ref = newSViv(0); obj = newSVrv(obj_ref, "Grief"); num_obj->num = ((Number*)SvIV(SvRV(arg_obj)))->num; sv_setiv(obj, (IV)num_obj); SvREADONLY_on(obj); return obj_ref; } void DESTROY(SV * obj) { printf("Destroying ..."); Number * number = (Number*)SvIV(SvRV(obj)); Safefree(number); printf("... destroyed\n"); } EOC $num1 = Grief::new(113); $num2 = $num1; $num3 = _copy($num1, '', ''); #Run any one of the following 4 lines $num1++; # _copy() gets called, 3 objects DESTROYed #$num2++; # _copy() gets called, 3 objects DESTROYed #_inc($num1, '', ''); # no _copy(), 2 objects DESTROYed #_inc($num2, '', ''); # no _copy(), 2 objects DESTROYed print _get_val($num2)," | ", _get_val($num3), "\n";

In reply to Re^4: Overloading '=' doesn't DWIM by syphilis
in thread Overloading '=' doesn't DWIM by syphilis

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.