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

Hi, all monks:

I encounted a strange problem with hash in Perl, I have write a tiny script to demostrate this problem:

#!/usr/bin/perl use strict; use warnings; my $heap = {}; $heap->{'172.16'} = 1; print "\$heap->{'172.16'}: $heap->{'172.16'}\n"; print "\$heap->{172.16}: $heap->{172.16}\n"; $heap->{'127.2'}{'127.1.0.2'} = 2; print "\$heap->{'127.2'}{'127.1.0.2'}: $heap->{'127.2'}{'127.1.0.2'}\ +n"; print "\$heap->{127.2}{127.1.0.2}: $heap->{127.2}{127.1.0.2}\n";
and here is the executed result:
$heap->{'172.16'}: 1 $heap->{172.16}: 1 $heap->{'127.2'}{'127.1.0.2'}: 2 Use of uninitialized value in concatenation (.) or string at /home/lar +ry/project/sysmonitor/test/testHash2.pl line 28. $heap->{127.2}{127.1.0.2}:
What surprise me is that $heap->{'127.2'}{'127.1.0.2'} is not the same as $heap->{127.2}{127.1.0.2}.

Can anyone give some tips?

  • Comment on What's the difference between $heap->{127.2}{127.1.0.2} and $heap->{'127.2'}{'127.1.0.2'} ?
  • Select or Download Code

Replies are listed 'Best First'.
Re: What's the difference between $heap->{127.2}{127.1.0.2} and $heap->{'127.2'}{'127.1.0.2'} ?
by tilly (Archbishop) on Dec 22, 2008 at 08:57 UTC
    127.1.0.2 is being interpreted as a version string. Search for "Version Strings" in perldata for details.
      Most particularly, see the first paragraph in the sub-section on 'Version Strings' in the section Scalar value constructors of perldata:
      Note: Version Strings (v-strings) have been deprecated. They will be removed in some future release after Perl 5.8.1. The marginal benefits of v-strings were greatly outweighed by the potential for Surprise and Confusion.
        And pre-version-strings it still would have not been the same as '127.1.0.2', since it would have been interpreted as (127.1) . (0.2), giving a key of '127.10.2'.
Re: What's the difference between $heap->{127.2}{127.1.0.2} and $heap->{'127.2'}{'127.1.0.2'} ?
by Bloodnok (Vicar) on Dec 22, 2008 at 13:26 UTC
    Further to tillys observation & recommendation, the general case is that if, when explicitly used, the name of the key contains a character in \W, then the key name requires enclosing in quotes in order that perl doesn't mis-interpret the actual meaning of the key name ... as tilly observes in this case.

    A user level that continues to overstate my experience :-))
Re: What's the difference between $heap->{127.2}{127.1.0.2} and $heap->{'127.2'}{'127.1.0.2'} ?
by Anonymous Monk on Dec 22, 2008 at 08:41 UTC
    Start your program with
    perl -MO=Deparse,-p foo.pl
    and compare the output to foo.pl
      the output like this:
      use warnings; use strict 'refs'; (my $heap = {}); ($$heap{'172.16'} = 1); print("\$heap->{'172.16'}: $$heap{'172.16'}\n"); print("\$heap->{172.16}: $$heap{172.16}\n"); ($$heap{'127.2'}{'127.1.0.2'} = 2); print("\$heap->{'127.2'}{'127.1.0.2'}: $$heap{'127.2'}{'127.1.0.2'}\n" +); print("\$heap->{127.2}{127.1.0.2}: $$heap{127.2}{127.1.0.2}\n");
      and have the same executed result:
      $heap->{'172.16'}: 1 $heap->{172.16}: 1 $heap->{'127.2'}{'127.1.0.2'}: 2 Use of uninitialized value in concatenation (.) or string at testHash3 +.pl line 25. $heap->{127.2}{127.1.0.2}: