Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: What's the point of this 'overload' idiom?

by ikegami (Patriarch)
on Dec 08, 2022 at 21:25 UTC ( [id://11148672]=note: print w/replies, xml ) Need Help??


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

Because

say $instance;
is nicer than
say $instance->as_string;

That's it. Because it's nice.

And it's not like it takes anything away. When's the last time you saw Class=HASH(0xDEADBEEF) and thought it was useful?[1]

But it's best to give the choice, though.

use overload '""' => \&as_string; sub as_string { my $self = shift; ... }

  1. The overload module provides a function to get that stringification if you really do need it. Also, the referenced var type, its address and the class associated with it are all available through Scalar::Util functions.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-16 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found