rumburak has asked for the wisdom of the Perl Monks concerning the following question:

use strict; use warnings; use utf8; use MIME::QuotedPrint; binmode STDOUT, ":encoding(UTF-8)"; my $str = "a\x{201E}z"; #my $str = "a„z"; #my $str = "a\x{201C}z"; #my $str = "a\x{03C9}z"; #my $str = "aäz"; #works print $str, "\n"; my $str_q = encode_qp($str); print $str_q, "\n";

gives me this error message / faulty output:

a„z Wide character in subroutine entry at ./x.pl line 18.

Only the fifth version of $str is working.... Can anyone tell me what is going here? (It is perl 5.24.1.)

Replies are listed 'Best First'.
Re: Wide character in subroutine entry using encode_qp
by ikegami (Patriarch) on Jun 23, 2017 at 17:55 UTC

    encode_qp expects encoded text ("the quoted-printable encoding is only defined for single-byte characters").

    use Encode qw( encode_utf8 ); my $encoded = encode_qp(encode_utf8($str));

    $ cat a.pl use strict; use warnings; use feature qw( say ); use utf8; use open ':std', ':encoding(UTF-8)'; use Encode qw( encode_utf8 ); use MIME::QuotedPrint qw( encode_qp ); for my $str ( "a\x{201E}z", "a„z", "a\x{201C}z", "a\x{03C9}z", "aäz", ) { say $str; my $str_q; if (!eval { $str_q = encode_qp($ARGV[0] ? encode_utf8($str) : $str); return 1; }) { warn($@); next; } print $str_q; }
    $ perl a.pl 0
    a„z
    Wide character in subroutine entry at a.pl line 22.
    a„z
    Wide character in subroutine entry at a.pl line 22.
    a“z
    Wide character in subroutine entry at a.pl line 22.
    aωz
    Wide character in subroutine entry at a.pl line 22.
    aäz
    a=E4z=
    
    $ perl a.pl 1
    a„z
    a=E2=80=9Ez=
    a„z
    a=E2=80=9Ez=
    a“z
    a=E2=80=9Cz=
    aωz
    a=CF=89z=
    aäz
    a=C3=A4z=
    

    Note that "aäz" didn't actually work; Perl was just unable to automatically detect the error.

      Thanks for such a good explanation! I am impressed!