> 1. Why would I want this particularly?

Operator overloading is a way to adapt operators (not functions) to an object class.

For example to define a different algebra° like complex numbers

Most operators in Perl are designed for number and string types.

Example: If you had $x and $i of a class "ComplexNumber" and wrote $i**$x without overloading, you'd only get the the result from the implicitly numified references. That's hardly useful ...

(Update: a more complex example for DateTime objects further down)

It can also help adding syntactic sugar to self defined internal DSLs.

But it's a complicated matter because you have to keep many side effects in mind when designing such an interface. Like precedence.

> 2. what's the specific thing in use in the code I'm seeing, use overload "" => sub {shift->name;}; and why would I want that?

Because otherwise the stringification of an object will be the string form of the ref-adress, which isn't helpful.

DEMO > perl -de0 DB<2> use IO::Handle; # just an arbitrary class from co +re DB<3> $io = IO::Handle->new(); DB<4> p "$io" # <-- Stringification IO::Handle=GLOB(0x32c5c70) DB<5> p 0+$io # <-- Numification 53238896 DB<6> p $io + $io # nummeric operation 106477792 DB<7> p $io . $io # string operation IO::Handle=GLOB(0x32c5c70)IO::Handle=GLOB(0x32c5c70) DB<8>

But overloading "" will make an object return something more meaningful instead in string context.

Like it's name from $self->name (sic)

HTH!˛ :)

Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery

°) (abstract) algebra means a set of things and allowed operations mapping set members to other members of the same set.

for instance integer arithmetic is an algebra different to real number arithmetics.

But division is tricky here, the result of 3/2 (integers) is 1.5 (real number) in Perl. Compare Python where 3/2 is 1, to keep the algebra clean.

This different interpretations could be fixed by overloading, tho I doubt that's easily done for native types in Perl.

Moreover, in Perl operators rule determine types, while in Python it's the other way round. Best (worst ;) example of mixing those two approaches is JS where 1+1 can occasionally be 11

a='1'; 1+a '11'

simply put: Overloading is a way to let types (classes) determine operators.

˛) That's a complicated but interesting subject. I tried my best to come up with good examples, please keep asking if you need more elaboration.

further update

have a look at the even more complicated overloading in DateTime#Overloading

my $new_dt = $dt + $duration_obj;

two classes are interacting here DateTime and DateTime::Duration

without overloading you'd need to use

$dt->add_duration($duration_object)

as you can see are overloaded operators a mean to DWIM.


In reply to Re: What's the point of this 'overload' idiom? (updated) by LanX
in thread What's the point of this 'overload' idiom? by Cody Fendant

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.