Why does opening a file in utf 8 mode change the behaviour of seek?
<p>Here's a simple text file with no special characters, called <code>utf-8.txt</code> with the following content.</p>

<pre><code>foo bar baz
one two three
</code></pre>

<p>The new line is following the unix convention (one byte), so that the entire size of the file is 26 = 11 + 1 + 13 + 1. (11 = <code>foo bar baz</code>, 13 = <code>one two three</code>.</p>

<p>If I read the file with the following perl script</p>

<pre><code>use warnings;
use strict;

open (my $f, '&lt;', 'utf8.txt');
&lt;$f&gt;;
seek($f, -4, 1);
my $xyz = &lt;$f&gt;;
print "$xyz&lt;";
</code></pre>

<p>it prints</p>

<pre><code>baz
&lt;
</code></pre>

<p>This is expected, since the <code>seek</code> command goes back four characters, the new line and the three belonging to <code>baz</code>.</p>

<p>If I now change the <code>open</code> statement to</p>

<pre><code>open (my $f, '&lt;:encoding(UTF-8)', 'utf8.txt');
</code></pre>

<p>the output changes to</p>

<pre><code> baz
&lt;
</code></pre>

<p>that is, the <code>seek</code> command goes back five characters (or it goes back four characters but skips the new line).</p>

<p>Is this behaviour expected? Is there a flag or somthing to turn this behaviour off?</p>

<p><strong>Edit</strong></p>

<p>As per <em>Andrzej A. Filip</em> suggestion, when I add <code>print join("+",PerlIO::get_layers($f)),"\n";</code> just after the <code>open</code> statement, it prints in the "normal" open case: <code>unix+crlf</code> and in the <code>open...encoding</code> case: <code>unix+crlf+encoding(utf-8-strict)+utf8</code>.</p>