Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re^3: Hash value typing

by haukex (Bishop)
on Jun 10, 2018 at 22:24 UTC ( #1216349=note: print w/replies, xml ) Need Help??

in reply to Re^2: Hash value typing
in thread Hash value typing

Why do the Perl developers (or others for that matter) consider it OK to change the internal respresentation of data (albeit the narrow context of Hash values) when it is referenced?

Well, to be fair, it's the internal representation. (BTW, it's not just hash values, scalars in general work this way.) All you see happening here is, as you said, Perl internally "caching" the results of its automatic and usually transparent conversion between strings and numbers - a major feature of the language. To a Perl programmer, 123 and "123" are (almost) always the same thing, and that's A Good Thing ;-)

I have just shown one case it matters!

Yes, one downside of the transparent conversion between strings and numbers inside Perl is that libraries that convert from Perl's data format to another have this issue to overcome (Data::Dumper, Data::Dump, the various JSON modules, ...). Some choose to always output it in one format, others use heuristics to decide whether something looks like a string or a number, and yet others try to look at Perl's internal representation, which as you've seen can change very easily. All of these ways have their flaws.


In Perl, figuring out the difference between 42 and "42" is nontrivial (and luckily, a Perl programmer usually doesn't have to care). If the consumer of Perl's output does happen to care, it's usually best to have some way to explicitly output one or the other.

Replies are listed 'Best First'.
Re^4: Hash value typing
by waynerasm (Initiate) on Jun 11, 2018 at 01:02 UTC

    BUG->no, lack of understanding on my part->yes (at least 'was' until you kindly remedied that)
    Thank you

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1216349]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2020-10-27 18:28 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (257 votes). Check out past polls.