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

... After only 3 hours searching, finally found a mistake in my code ...

I am somewhat disappointed actually getting no warnings from the compiler - please consider the following code:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash; $hash{'A'.'B'} = 1; # note a mistakenly written comma between 'C' and 'D' $hash{'C','D'} = 2; $hash{'E','F'} = 3; print Dumper(\%hash); print exists $hash{'CD'} ? "YES\n" : "NO\n";
And here is the output
$VAR1 = { 'CD' => 2, 'EF' => 3, 'AB' => 1 }; NO
My xterm shows nothing between C and D in the output from Dumper, but with hexdump i could see that there is actually HEX: '1c' there
24 56 41 52 31 20 3d 20 7b 0a 20 20 20 20 20 20 |$VAR1 = {. | 20 20 20 20 27 43 1c 44 27 20 3d 3e 20 32 2c 0a | 'C.D' => 2,.| 20 20 20 20 20 20 20 20 20 20 27 45 1c 46 27 20 | 'E.F' | 3d 3e 20 33 2c 0a 20 20 20 20 20 20 20 20 20 20 |=> 3,. | 27 41 42 27 20 3d 3e 20 31 0a 20 20 20 20 20 20 |'AB' => 1. | 20 20 7d 3b 0a 4e 4f 0a | };.NO.|
Any ideas, what's really happened? Why is it '1c' and not 'f5'? Is it a bug that perl produces no warnings in this case?

Replies are listed 'Best First'.
Re: Comma Operator, BUG or feature ???
by moritz (Cardinal) on Sep 22, 2009 at 15:35 UTC
    use
    use Data::Dumper; $Data::Dumper::Useqq = 1; ...

    To properly escape non-printing characters in the output.

    See the documentation for $; to see what's happening.

    Perl 6 - links to (nearly) everything that is Perl 6.
      Thank you for the explanation - I didn't know that $foo{$a,$b,$c} means $foo{join($;, $a, $b, $c)} Thank you again
Re: Comma Operator, BUG or feature ???
by ikegami (Patriarch) on Sep 22, 2009 at 16:01 UTC

    Is it a bug that perl produces no warnings in this case?

    "." is the concatenation operator, and you used it properly. There's no reason for it to give a warning.

    As for a hash element index, it accepts multiple comma-separated values. This was a means of doing multilevel hashes before Perl had true references. These days, one would create a hash or hashes.

    $hash{A}{B} = 1; $hash{C}{D} = 2; $hash{E}{F} = 3;

    Again, it was used properly, so there's no reason for it give a warning.

    Why is it '1c' and not 'f5'?

    F5? As in LATIN SMALL LETTER O WITH TILDE? Why do you expect that?

    It can be done, though. Just change $; to "\xF5".

Re: Comma Operator, BUG or feature ???
by JavaFan (Canon) on Sep 22, 2009 at 17:13 UTC
    You might want to look up the $; entry in perlvar.

    $hash{'C','D'} is short for $hash{join($;,'C','D')}, with $; defaulting to "\034", which is the same as "\x1c".