title: Why does substr not return undef at the end of a string?
body: <p>I'm not sure whether this is defined behaviour or not. I have the following code:</p>
  <pre><code>use strict;
  use warnings;
  use Data::Dumper;
  
  my $string = 'aaaaaa0aaaa';
  my $char = substr($string, length($string), 1);
  my $char2 = substr($string, length($string)+1, 1);
  
  print Dumper($char);
  print Dumper($char2);
  </code></pre>
  <p>Besides getting one warning about <code>substr()</code> past the end of a string, I'm confused about the output:</p>
  <pre><code>$VAR1 = '';
  $VAR1 = undef;
  </code></pre>
  <p><a href="http://perldoc.perl.org/functions/substr.html" rel="noreferrer">Perldoc</a> says about <code>substr</code>:</p>
  <blockquote>
  <p>substr EXPR,OFFSET,LENGTH</p>
  <p>If OFFSET and LENGTH specify a substring that is partly outside the string, only the part within the string is returned. If the substring is beyond either end of the string, substr() returns the undefined value and produces a warning.</p>
  </blockquote>
  <p>Both <code>length($string)</code> and <code>length($string) + 1</code> are beyond the (zero-indexed) end of the string, so I don't know why <code>substr</code> returns the empty string in one case and <code>undef</code> in the other. Does it have to do with the NULL character that C uses for string termination and that is somehow returned by <code>substr</code> in the first case, so that there is an &quot;invisible&quot; last character to this string that is not counted by <code>length</code>? Am I missing something obvious here?</p>