Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

"Default" method

by v_melnik (Scribe)
on Oct 08, 2014 at 12:15 UTC ( #1103159=perlquestion: print w/replies, xml ) Need Help??

v_melnik has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I saw a cool trick that XML::LibXML, Throwable and some other libraries can do: when you try to get the value of the variable containing a reference to the object, you get the result of calling one of its' methods (for example as_string() instead of "SomeClass=HASH(0xdeadbeef)".

How to do that?

Thank you!

UPD: Thank you for replies, the "overload" pragma is exactly what I need!

V.Melnik

Replies are listed 'Best First'.
Re: "Default" method
by McA (Priest) on Oct 08, 2014 at 12:17 UTC

    Hi,

    you can do that with overloading the stringification operator in your class.

    use overload ( '""' => sub { shift->to_string; }, );

    where to_string is the method returning a string representation.

    Regards
    McA

Re: "Default" method
by Anonymous Monk on Oct 08, 2014 at 12:18 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1103159]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2023-12-05 04:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (25 votes). Check out past polls.

    Notices?