in reply to Can I ask Perl if an object will stringify?
I think that is probably non-trivially difficult to do as you're thinking; and anyway, is not the best way to be approaching it. I think, instead, you should implement the constraint (the contract) as permissively as possible, and that means trying to use the argument as a string, and simply letting the chips fall where they may. (That is, you could catch them and issue your own warning; or let them bounce up the stack, or whatever.) You've already identified one scenario where a simple can-like test fails to meet the need; there could certainly be others. For example, the argument could be a ref to a tied object whose FETCH method returns a stringifiable or nonstringifiable value depending on program state. (At least, I think that's a possible scenario; I haven't tested it.) The point is, don't try to predict and handle all the possibilities.
Btw... I think forcing stringification by concatenation ( $s = ''.$s;) is probably more efficient than by interpolation ( $s = "$s";).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Can I ask Perl if an object will stringify?
by haukex (Archbishop) on May 25, 2015 at 09:48 UTC |