Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^6: How to know to know if string is utf8 encoded or decoded.

by 1nickt (Abbot)
on Jul 26, 2019 at 13:42 UTC ( #11103443=note: print w/replies, xml ) Need Help??


in reply to Re^5: How to know to know if string is utf8 encoded or decoded.
in thread How to know to know if string is utf8 encoded or decoded.

I don't understand. Why do you think that Encode::decode('MIME-Header',$str) is going to do anything useful?

$ perl -Mstrict -MEncode -wE 'my $str="foo"; say encode("MIME-Header", + $str);' =?UTF-8?B?Zm9v?=


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^7: How to know to know if string is utf8 encoded or decoded.
by Anonymous Monk on Jul 26, 2019 at 15:14 UTC

    Thank you for the reply.
    That is existing code. basically the logic behind the code is, if filename is utf8 encoded then return the string as it is but if it is not then decode it using encode::decode.

    for filename "test1℗ὓ.txt" there is not need to decode at all but with filename "1669-SCC-H˘pitauxdeSaint-Maurice-POC.PIF", it is not working with or without decode. when I say not working it is not getting decoded properly so that file is not getting attached.

      You have already been given your answers; I am sorry you are struggling to hear them. choroba showed that the strings can be decoded fine, and I explained that you are using a bad value for the encoding.


      #!/usr/bin/perl
      use strict;
      use warnings;
      use feature 'say';
      use utf8;
      
      use Encode;
      
      my $str = '1669-SCC-H˘pitauxdeSaint-Maurice-POC.PIF';
      
      say 'UTF-8: ' . encode('UTF-8', $str);
      say 'MIME-Header: ' . encode('MIME-Header', $str);
      
      $str = 'test1℗ὓ.txt';
      
      say 'UTF-8: ' . encode('UTF-8', $str);
      say 'MIME-Header: ' . encode('MIME-Header', $str);
      
      __END__
      

      $ perl foo.pl
      UTF-8: 1669-SCC-H˘pitauxdeSaint-Maurice-POC.PIF
      MIME-Header: =?UTF-8?B?MTY2OS1TQ0MtSMO0cGl0YXV4ZGVTYWludC1NYXVyaWNlLVBPQy5QSUY=?=
      UTF-8: test1℗ὓ.txt
      MIME-Header: =?UTF-8?B?dGVzdDHihJfhvZMudHh0?=
      


      The way forward always starts with a minimal test.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11103443]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2020-04-03 21:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (32 votes). Check out past polls.

    Notices?