title: SOLVED: Hash content access is inconsistent with different perl version
body: <p>I came across an interesting problem with following piece of code in <a href="https://rextester.com/l/perl_online_compiler" rel="nofollow noreferrer">perl 5.22.1</a> and <a href="https://www.jdoodle.com/execute-perl-online/" rel="nofollow noreferrer">perl 5.30.0</a></p>
  
  <pre><code>use strict;
  use warnings;
  use feature 'say';
  
  #use Data::Dumper;
  
  my %hash;
  my %seen;
  my @header = split ',', &lt;DATA&gt;;
  
  chomp @header;
  
  while(&lt;DATA&gt;) {
      next if /^\s*$/;
      chomp;
      my %data;
      @data{@header} = split ',';
  
      push @{$hash{person}}, \%data;
      push @{$hash{Position}{$data{Position}}}, "$data{First} $data{Last}";
      if( ! $seen{$data{Position}} ) {
          $seen{$data{Position}} = 1;
          push @{$hash{Role}}, $data{Position};
      }
  }
  
  #say Dumper($hash{Position});
  
  my $count = 0;
  for my $person ( @{$hash{person}} ) {
      say "Person: $count";
      say "Role: $person-&gt;{Position}";
  }
  
  say "---- Groups ----\n";
  
  while( my($p,$m) = each %{$hash{Position}} ) {
      say "-&gt; $p";
      my $members = join(',',@{$m});
      say "-&gt; Members: $members\n";
  }
  
  say "---- Roles ----";
  
  say '-&gt; ' . join(', ',@{$hash{Role}});
  
  __DATA__
  First,Last,Position
  John,Doe,Developer
  Mary,Fox,Manager
  Anna,Gulaby,Developer
  </code></pre>
  
  <p>If the code run as it is -- <a href="https://rextester.com/DLP55385" rel="nofollow noreferrer">everything works fine</a></p>
  
  <p>Now it is sufficient to add <code>$count++</code> increment as bellow and <a href="https://rextester.com/DSW61156" rel="nofollow noreferrer">code produces errors</a></p>
  
  <pre><code>my $count = 0;
  for my $person ( @{$hash{person}} ) {
      $count++;
      say "Person: $count";
      say "Role: $person-&gt;{Position}";
  }
  </code></pre>
  
  <p>Errors:</p>
  
  <pre><code>Error(s), warning(s):
  Use of uninitialized value $data{"Position"} in hash element at source_file.pl line 22, &lt;DATA&gt; line 2.
  Use of uninitialized value $data{"Position"} in hash element at source_file.pl line 23, &lt;DATA&gt; line 2.
  Use of uninitialized value $data{"Position"} in hash element at source_file.pl line 24, &lt;DATA&gt; line 2.
  Use of uninitialized value $data{"Position"} in hash element at source_file.pl line 22, &lt;DATA&gt; line 3.
  Use of uninitialized value $data{"Position"} in hash element at source_file.pl line 23, &lt;DATA&gt; line 3.
  Use of uninitialized value $data{"Position"} in hash element at source_file.pl line 22, &lt;DATA&gt; line 4.
  Use of uninitialized value $data{"Position"} in hash element at source_file.pl line 23, &lt;DATA&gt; line 4.
  Use of uninitialized value in concatenation (.) or string at source_file.pl line 35, &lt;DATA&gt; line 4.
  Use of uninitialized value in concatenation (.) or string at source_file.pl line 35, &lt;DATA&gt; line 4.
  Use of uninitialized value in concatenation (.) or string at source_file.pl line 35, &lt;DATA&gt; line 4.
  Use of uninitialized value in join or string at source_file.pl line 48, &lt;DATA&gt; line 4.
  </code></pre>
  
  <p>This problem does not manifest itself in perl 5.30.0 (Windows 10, Strawberry Perl) or <a href="https://www.tutorialspoint.com/execute_perl_online.php" rel="nofollow noreferrer">Perl v5.24.2</a>.</p>
  
  <p><strong>Note:</strong> the problem manifests itself not only with <code>$count++</code> but with any other access to content of the hash next to <code>say "Person: $count";</code> -- <a href="https://stackoverflow.com/questions/60653651/process-an-array-and-group-rows-by-one-field-from-a-csv/60662118?noredirect=1#comment107371985_60662118">post# 60653651</a></p>
  
  <p>I would like to hear comments on this situation, what is the cause?</p>
  
  <p><strong>CAUSE:</strong> input data have <em>eol</em> in DOS form <code>\r\n</code> and when data processed in <em>Linux</em> <code>chomp</code> removes only <code>\n</code> leaving <code>\r</code> as part of the field name (used as hash key). Thanks goes to <a href="https://stackoverflow.com/users/9952196/shawn">Shawn</a> for pointing out the source of the issue. </p>
  
  <p><strong>SOLUTION:</strong> universal <a href="https://rextester.com/XBSW79657" rel="nofollow noreferrer">fix</a> was implemented in form of <code>snip_eol($arg)</code> subroutine</p>
  
  <pre><code>use strict;
  use warnings;
  use feature 'say';
  
  my $debug = 0;
  
  say "
  Perl:  $^V
  OS: $^O
  -------------------
  " if $debug;
  
  my %hash;
  my %seen;
  my @header = split ',', &lt;DATA&gt;;
  
  $header[2] = snip_eol($header[2]);        # problem fix
  
  while(&lt;DATA&gt;) {
      next if /^\s*$/;
  
      my $line = snip_eol($_);              # problem fix
  
      my %data;
      @data{@header} = split ',',$line;
  
      push @{$hash{person}}, \%data;
      push @{$hash{Position}{$data{Position}}}, "$data{First} $data{Last}";
      if( ! $seen{$data{Position}} ) {
          $seen{$data{Position}} = 1;
          push @{$hash{Role}}, $data{Position};
      }
  }
  
  #say Dumper($hash{Position});
  
  my $count = 0;
  for my $person ( @{$hash{person}} ) {
      $count++;
      say "-&gt; Name:   $person-&gt;{First} $person-&gt;{Last}";
      say "-&gt; Role:   $person-&gt;{Position}\n";
  }
  
  say "---- Groups ----\n";
  
  while( my($p,$m) = each %{$hash{Position}} ) {
      say "-&gt; $p";
      my $members = join(',',@{$m});
      say "-&gt; Members: $members\n";
  }
  
  say "---- Roles ----";
  
  say '-&gt; ' . join(', ',@{$hash{Role}});
  
  sub snip_eol {
      my $data = shift;                      # problem fix
  
      #map{ say "$_ =&gt; " . ord } split '', $data if $debug;
      $data =~ s/\r// if $^O eq 'linux';
      chomp $data;
      #map{ say "$_ =&gt; " . ord } split '', $data if $debug;
  
      return $data;
  }
  
  __DATA__
  First,Last,Position
  John,Doe,Developer
  Mary,Fox,Manager
  Anna,Gulaby,Developer
  </code></pre>