Is there a way to test if a scalar has been stringified or not?

Posted by Yobert on Stack Overflow See other posts from Stack Overflow or by Yobert
Published on 2012-03-20T20:57:02Z Indexed on 2012/03/20 23:29 UTC
Read the original article Hit count: 145

Filed under:

I am writing a thing to output something similar to JSON, from a perl structure. I want the quoting to behave like this:

"string" outputs "string"
"05" outputs "05"
"5" outputs "5"
5 outputs 5
05 outputs 5, or 05 would be acceptable

JSON::XS handles this by testing if a scalar has been "stringified" or not, which I think is very cool. But I can't find a way to do this test myself without writing XS, which I'd rather avoid. Is this possible? I can't find this anywhere on CPAN without finding vast pedantry about Scalar::Util::looks_like_number, etc which completely isn't what I want. The only stopgap I can find is Devel::Peek, which feels evil. And also, just like JSON::XS, I'm fine with this secenario:

my $a = 5;
print $a."\n";
# now $a outputs "5" instead of 5)

© Stack Overflow or respective owner

Related posts about perl