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

Hi,

Methinks you are trying to break the language itself: perl stringifies everything on demand (in string context). The "stringification" operator purpose is to provide something meaningful, and not to be some marker "it stringifies to to something meaningful".

I think you can use something like UNIVERSAL::can to check pretense of method like "to_my_string". If the passed object don't have that method, that means that something is wrong, and die could be better then warn.

WBR, basiliscos.
  • 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 haukex (Archbishop) on May 25, 2015 at 10:12 UTC

    Hi basiliscos,

    Thanks for your response. Just to clarify: The values I'm looking at are not under my control. They are normally supposed to be numbers, strings, or objects that stringify to something useful, like for example Path::Class objects. Those values will be stringified and used as an argument to an external command, which is why I make the assumption that references, which would be stringified to something like "HASH(0x9c5d2d9)", are probably a mistake on the user's part, and I'd like to provide a warning for those cases. I've posted my current solution below.

    Thanks and Regards,
    -- Hauke D