Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Hash key composition with a comma?

by Anonymous Monk
on Mar 17, 2018 at 03:21 UTC ( [id://1211096]=note: print w/replies, xml ) Need Help??


in reply to Hash key composition with a comma?

It may have been a nice idea – back then – but you really should eliminate all such things from your code today.   Because, sooner or sooner, your actual-data is going to include an instance of that "separator" and ... congratulations, you've just been waked-up at three o'clock in the morning when your pager went off, and ushered into Debugging Triage Hell. When you encounter such logic, it should be promptly refactored into a HoH = Hash of Hashes re-implementation.

Replies are listed 'Best First'.
Re^2: Hash key composition with a comma?
by Your Mother (Archbishop) on Mar 17, 2018 at 04:47 UTC

    Perhaps you could show how such a legacy code trap might work. Some code to demonstrate the issue.

      It's not at all hard to see it – an example is not necessary. The perldoc quoted in the first reply says everything: the trick works by means of join(), such that it would have been far better if the feature had not been a part of the language and programmers had to type a few extra letters into their 300-baud teletypes. If your data contains the separator-character, the code breaks. Whereas, if you (today) use hash-of-hashes and auto-vivification, you can quickly construct real multi-dimensional data structures without concern for what the hash-key values might be, and the code is no more cumbersome. Some of the "older" ideas in Perl were really good ones. But this is not one of them.
        It's a variation of the Semipredicate problem.

        Another variation are Null-terminated strings in C. *

        In other words, if you use a "special" value (here the delimiter) you have to make sure that the input (here keys) can't include them, and that's why you even have the freedom to chose the delimiter by setting $;

        Such approaches happen constantly in programming and people make sure that such values are excluded, IFF they need the benefits. (For example by escaping or the choice of delimiter in q , qq or even << here-docs.)

        Calling for deprecation doesn't make sense here, since one doesn't easily and accidentally stumble over this feature.

        It's similar to goto , it's rarely used, doesn't cause problems, but is sometimes extremely handy to simplify an algorithm, where speed matters.

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

        *) and yes I once got bitten by this because a badly written WMLScript interpreter couldn't distinguish between a NULL byte and the ASCII character "0". But this interpreter was buggy, there was otherwise no possibility to introduce a NULL-byte into a string.

        An example, AKA proof, is actually necessary when one makes an assertion in the sciences. That you bloviated instead of backing up the statement is plenty of proof for my own assertions.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-20 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found