This problem is unrelated to the use of "decode()" shown in the OP script here

You're right. I thought binmode($untrusted_fh, ':utf8') was the same as decode('utf8', $untrusted), but it's the same as _utf8_on($untrusted).

use strict; use warnings; use Encode qw( decode _utf8_on ); use Devel::Peek; my $bytes = "\x{C2}A"; { open(my $fh, '>:raw', 'temp') or die; print $fh $bytes; } { print STDERR "_utf8_on\n"; print STDERR "--------\n"; open(my $fh, '<:raw', 'temp') or die; _utf8_on(my $chars = <$fh>); Dump($chars); } print STDERR "\n"; { print STDERR "binmode ':utf8'\n"; print STDERR "---------------\n"; open(my $fh, '<:utf8', 'temp') or die; my $chars = <$fh>; Dump($chars); } print STDERR "\n"; { print STDERR "binmode ':encoding(utf8)'\n"; print STDERR "-------------------------\n"; open(my $fh, '<:encoding(utf8)', 'temp') or die; my $chars = <$fh>; Dump($chars); } print STDERR "\n"; { print STDERR "decode 'utf8'\n"; print STDERR "-------------\n"; my $chars = decode('utf8', $bytes); Dump($chars); } print STDERR "\n"; { print STDERR "decode 'utf-8'\n"; print STDERR "--------------\n"; my $chars = decode('utf-8', $bytes); Dump($chars); } unlink 'temp';
_utf8_on -------- SV = PV(0x226154) at 0x1853e20 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK,UTF8) PV = 0x18307d4 "\302A"\0Malformed UTF-8 character (unexpected non-co +ntinuation byte 0x41, immediately after start byte 0xc2) in subroutin +e entry at 688773.pl line 19, <$fh> line 1. [UTF8 "\x{0}"] CUR = 2 LEN = 80 binmode ':utf8' --------------- utf8 "\xC2" does not map to Unicode at 688773.pl line 28, <$fh> line 1 +. SV = PV(0x226154) at 0x18dcdc4 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK,UTF8) PV = 0x183083c "\302A"\0Malformed UTF-8 character (unexpected non-co +ntinuation byte 0x41, immediately after start byte 0xc2) in subroutin +e entry at 688773.pl line 29, <$fh> line 1. [UTF8 "\x{0}"] CUR = 2 LEN = 80 binmode ':encoding(utf8)' ------------------------- utf8 "\xC2" does not map to Unicode at 688773.pl line 38. SV = PV(0x22616c) at 0x18dce6c REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK,UTF8) PV = 0x18f2964 "\\xC2A"\0 [UTF8 "\\xC2A"] CUR = 5 LEN = 80 decode 'utf8' ------------- SV = PV(0x22616c) at 0x18dce9c REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK,UTF8) PV = 0x182d0cc "\357\277\275A"\0 [UTF8 "\x{fffd}A"] CUR = 4 LEN = 8 decode 'utf-8' -------------- SV = PV(0x18d1688) at 0x18dcefc REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK,UTF8) PV = 0x182ce5c "\357\277\275A"\0 [UTF8 "\x{fffd}A"] CUR = 4 LEN = 8

In reply to Re^3: CGI hidden params vs. character encoding by ikegami
in thread CGI hidden params vs. character encoding by graff

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.