in reply to How to know to know if string is utf8 encoded or decoded.

What do you mean by "is working"? The following works when the script is saved as UTF-8 and run in a UTF-8 terminal:
#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
use utf8;
 
use Encode;
 
my @strings = ('test1℗ὓ.txt', '1669-SCC-HôpitauxdeSaint-Maurice-POC.PIF');
 
for my $string (@strings) {
    say encode('UTF-8', $string);
}

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: How to know to know if string is utf8 encoded or decoded.
by Anonymous Monk on Jul 25, 2019 at 16:23 UTC

    Thank you for the reply.
    below is the line which seems to have issue.
    eval {$result = ConvertEncoding($string,"utf8",'MIME-Header')}
    #convertEncoding uses below code to decode string

    eval { $unicode = Encode::decode($from,$str); }; if ($@) { &ConvertEncodingError("($from -> utf8)\n$@"); return $str; }

      Wrapping the code in eval only makes sense if you ask decode to die if it can't decode:
      eval { $unicode = Encode::decode($from,$str,Encode::FB_CROAK); }; if ($@) { &ConvertEncodingError("($from -> utf8)\n$@"); return $str; }
      You should be aware that in case of a decoding error, $str will be overwritten.

        The third arg needs to be Encode::FB::CROAK | Encode::LEAVE_SRC since you don't want $from to change.

      eval { $unicode = Encode::decode($from,$str); #here $from is +'MIME-Header' }; if ($@) { &ConvertEncodingError("($from -> utf8)\n$@"); return $str; }

        "here $from is 'MIME-Header'"

        Shouldn't it be 'utf8'?


        The way forward always starts with a minimal test.