in reply to What's the point of this 'overload' idiom?

Perl is all about "if you treat it as a string, it'll try to behave as a string". If you treat a number as a string, it'll give you the number in decimal form; if you treat an object as a string, it will give you the class, reftype and refaddr (like Object=HASH(0x559ee79cc1e0)).

Some objects are intended to behave like other things, for example Math::BigInt objects are intended to behave like numbers, so when they are stringified you don't want them to show Math::BigInt=HASH(0x559ee79cc1e0), you want them to stringify as a number the same as a normal integer would. That's what this use of overload is doing.

Overloading means you can do this:

% perl -MMath::BigInt -E 'my $x = Math::BigInt->new(2)**100; say $x' 1267650600228229401496703205376 %

There is overloading of the ** operator going on here, and then stringification overload for the say.

Overloading as a general concept is something you want very rarely, but is very useful when you do want it. Once you are aware of it, the main skill is knowing to use it very sparingly - it is usually a really bad idea to have your work code overload the Bank::Account class to make $account += $amount credit the account. But I happen to write a lot of maths code with big integers, and for that context it is incredibly useful.

Replies are listed 'Best First'.
Re^2: What's the point of this 'overload' idiom?
by stevieb (Canon) on Dec 07, 2022 at 05:13 UTC
    Overloading as a general concept is something you want very rarely, but is very useful when you do want it.

    I really like that statement. Well put.

    One area where overloading is highly desired is during unit testing. Although I'd classify it as 'overwriting' rather than overloading, the premise is the same. Make a function do something different than what the original does. For example, Mock::Sub does exactly this. Overriding functions during testing happens very often and is hugely useful.

      I might be wrong, but I think the technical terms for overwriting functions are I'd use overloading explicitly for perlops

      But this might be restricted to Perl lingo and different in other languages (the line between operators, built-ins and functions is blurred and depends on context)

      And I have to admit that I'm anal about terminology, a "maladie professionnelle" of studying math...

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

Re^2: What's the point of this 'overload' idiom?
by Cody Fendant (Hermit) on Dec 07, 2022 at 07:01 UTC

    Thanks for that. If you don't mind, what's the point of the specific thing where it's not a code reference in there but shift->name;?

      shift->name

      Is a lazy man's way of saying:

      sub thing { my ($self) = @_; return $self->name; }

      In other words, it's a quick way of not declaring the 'self' variable. I do it a different way if I have a one line sub:

      sub name { return $_[0]->{name}; }

      ...the following is equivalent:

      sub name { return shift->{name}; }

      When a Perl method receives its parameters, the first one is always the calling object. shift or $_[0] is the same thing; the object itself. If a method is more than one line, I prefer to collect the parameter as self (eg. my ($self) = @_;). That's not always the case.

      Note that shift->thing; only works once... once you've shifted the object off the stack, you can't call shift again. In these cases, $_[0] is better, but if you have to use $_[0] more than once, you're far better off for self-documenting purposes to use my ($self) = @_;.