Substr in Perl, cutting utf8 characters in half
<p>I'm trying to figure out a problem when using <code>substr</code> in Perl v5.8.5. Here is an example string:</p>

<blockquote>
  <p>טוב בגןUTF-8 Examples עץ טוב בגן いろはにほξεσκεπάζω ff</p>
</blockquote>

<pre><code>if (length($hit-&gt;{post_title}) &gt; 60) {
  $hit-&gt;{post_title} = substr($hit-&gt;{post_title},0,60);
  $hit-&gt;{post_title} .= "...";
}
</code></pre>

<p>Seems to get trimmed to:</p>

<blockquote>
  <p>UTF-8 Examples עץ טוב בגן いろはにほξεσκε�...</p>
</blockquote>

<p>In the code, I see a message about a wide character. I'm wondering if maybe <code>substr</code> is somehow cutting the word in half? If I remove the <code>substr()</code> part of the code, then the string shows up fine.</p>

<p>I've never come across this with utf8 before. Do we need to do something special with arabic characters? </p>

<p>UPDATE: If I check for the length of the strings before and after, something funky does seem to be going on:</p>

<blockquote>
  <p>TEST: UTF-8 Examples עץ טוב בגן いろはにほξεσκεπάζω<br>
  LENGTH: 63
  LENGTH: 67 AFTER</p>
</blockquote>

<p>I'm just not sure what! FWIW, I'm also using this in the header of the script:</p>

<pre><code>use utf8;
</code></pre>

<p>Thanks! </p>