http://www.perlmonks.org?node_id=1211078

mpersico has asked for the wisdom of the Perl Monks concerning the following question:

Just when I thought I had seen everything:
my %hash; my $foo = 'hello'; my $bar = 'world'; my $baz = 'folk'; $hash{ $foo, $bar, $baz } = '##eek what magic is this?'; print $hash{"$foo\x{1c}$bar\x{1c}$baz"};
##eek what magic is this?

By what rule are multiple variables, separated by a comma, turned into a string with a File Separator between the pieces? I mean if I squint hard enough, that expression for the key is a list, and then the list is "stringified" to be a key, in which case I would have expected a space (\x{20}) separated expression. Perl 5.16 and Perl 5.26.1

Replies are listed 'Best First'.
Re: Hash key composition with a comma?
by LanX (Saint) on Mar 16, 2018 at 20:18 UTC
Re: Hash key composition with a comma?
by pryrt (Abbot) on Mar 16, 2018 at 20:16 UTC
Re: Hash key composition with a comma?
by pwagyi (Monk) on Mar 17, 2018 at 08:00 UTC

    It is mostly used in legacy code as answered before.

    If you are familiar with awk, it is the *way* to emulate multi-dimensional hash in awk/ and perl without reference

    I admit I've used a bit in some places where the use is well hidden and localized. You should prefer proper multi-dimensional hash (if it's really what you need). imagine to get keys of hash{foo} in emulated hash version, it'd be very difficult to do so.

    # with proper multi-dimensional hash keys(%{$hash{foo}}); # get keys of hash{foo}
Re: Hash key composition with a comma?
by Anonymous Monk on Mar 16, 2018 at 23:27 UTC
Re: Hash key composition with a comma?
by Anonymous Monk on Mar 17, 2018 at 03:21 UTC
    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.

      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.
A reply falls below the community's threshold of quality. You may see it by logging in.