in reply to Can I ask Perl if an object will stringify?

Strange that nobody brought up Devel::OverloadInfo. It will do exactly what you need (unless I misunderstood your question).
  • Comment on Re: Can I ask Perl if an object will stringify?

Replies are listed 'Best First'.
Re^2: Can I ask Perl if an object will stringify?
by LanX (Saint) on Apr 01, 2015 at 10:19 UTC
    > Strange that nobody brought up ...

    Because nobody compares to you, you son of a rabbit. ;)

    Thanks++ :)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    PS: Je suis Charlie!

Re^2: Can I ask Perl if an object will stringify?
by haukex (Archbishop) on May 25, 2015 at 10:36 UTC

    Hi ribasushi,

    Thanks for the suggestion! I tried it out, and the module returns a bit more information than overload::Method(), but unless I missed something it unfortunately doesn't seem to directly answer the question whether an object will stringify via magic autogeneration or not, but I could probably figure it out based on all the information it returns. Here's an example of what the overload_info() function returns for two of my test classes from the original post:

    # this class stringifies even though "" is not directly overloaded { "0+" => { class => "ICanStringify", code => sub { ... }, code_name => "ICanStringify::__ANON__", }, "fallback" => { class => "ICanStringify", value => undef }, } # this class does not stringify { "0+" => { class => "OnlyANumber", code => sub { ... }, code_name => "OnlyANumber::__ANON__", }, "fallback" => { class => "OnlyANumber", value => 0 }, }

    Right now my current solution (posted below) of "just try it out" seems to work, so I'm a bit hesitant to change it except in a way that would simplify it. I'll look into it a bit more whether I can do so with the above info.

    Thanks and Regards,
    -- Hauke D