Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Hash key composition with a comma?

by Your Mother (Archbishop)
on Mar 17, 2018 at 04:47 UTC ( [id://1211099]=note: print w/replies, xml ) Need Help??


in reply to Re: Hash key composition with a comma?
in thread Hash key composition with a comma?

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

  • Comment on Re^2: Hash key composition with a comma?

Replies are listed 'Best First'.
Re^3: Hash key composition with a comma?
by Anonymous Monk on Mar 17, 2018 at 13:34 UTC
    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.

        I second you, cause it's very hard to construct a scenario where multi dim hashes cause a bug, even if the programmer was so naive to allow random keys.

        And the statement:

        > If your data contains the separator-character, the code breaks. 

        Is not true. The constructed key would have more separator characters but wouldn't "break".

        I'd really like to see a believable example, maybe I am missing something?

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

        An example, AKA proof, is actually necessary when one makes an assertion in the sciences.

        Firstly, examples are not proofs "in the sciences" unless they are counterexamples.

        Secondly, this is misplaced pedantry. It really is quite obvious that, when you rely on metacharacters, your data had better not inadvertently include them. This is not an assertion that needs to be proven to anybody with a modicum of programming experience. AM was entirely correct on that point.

        I'll agree that AM's admonition against using the feature was hyperbolic. Sometimes it is better to avoid the pitfalls of some language feature than it is to avoid the feature itself. And we are entirely capable of doing so. (As Rolf points out.)

        -sauoq
        "My two cents aren't worth a dime.";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 03:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found