The is_utf8
function (whether it is from utf8
or from Encode
) just tells you whether a string has the internal UTF8
flag set. That is pretty much a consequence of what you have said it contains yourself, and is very different from it being valid UTF-8.
If you want to check the capabilities of a file handle then you should take a look at the PerlIO::Layers
module. A call like
query_handle(*STDIN, 'utf8')
will return true if the handle is UTF-8-capable, by setting either :utf8
or :encoding(utf8)
.
If you want to check specifically for :encoding(utf8)
then you need
query_handle(*STDIN, 'layer', 'encoding')
but note that this will show only whether there is an :encoding()
layer of any sort, which could be :encoding(iso-8859-1)
.
If you really need to check which encoding is in place, the only way I know is to examine the return value of get_layers
from the same module. It returns a list of arrays corresponding to the PerlIO layers in effect on the handle. Something like this
(
["unix", undef, ["CANREAD", "OPEN"]],
["encoding", "utf8", ["FASTGETS", "CANREAD", "LINEBUF", "UTF8"]],
)