jwkrahn has asked for the wisdom of the Perl Monks concerning the following question:
My original code:
use utf8; use open ':std', ':utf8'; use feature 'unicode_strings'; ... for my $file ( @files ) { open my $FH, '<', $file or die "Cannot open '$file' because: $!"; my $size = read $FH, my $data, -s $FH or die "Cannot read from '$f +ile' because: $!"; $size == length( $data ) or die "Error reading from '$file'\n"; close $FH; ...
This will always result (if $data contains Unicode characters) in dieing with the message "Error reading from '$file'\n".
The solution is to use this instead:
$size == $data =~ y///c or die "Error reading from '$file'\n";
Hope this helps.
Please correct me if I am wrong.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Counting bytes in a Unicode document
by hippo (Archbishop) on Oct 06, 2024 at 21:10 UTC | |
Re: Counting bytes in a Unicode document
by parv (Parson) on Oct 07, 2024 at 07:30 UTC | |
Re: Counting bytes in a Unicode document
by LanX (Saint) on Oct 06, 2024 at 21:11 UTC | |
by ysth (Canon) on Oct 08, 2024 at 01:11 UTC | |
by LanX (Saint) on Oct 08, 2024 at 10:01 UTC | |
by ikegami (Patriarch) on Oct 08, 2024 at 15:49 UTC | |
by LanX (Saint) on Oct 08, 2024 at 17:12 UTC | |
| |
Re: Counting bytes in a Unicode document
by ikegami (Patriarch) on Oct 08, 2024 at 15:52 UTC | |
by LanX (Saint) on Oct 08, 2024 at 17:17 UTC | |
by ikegami (Patriarch) on Oct 09, 2024 at 00:32 UTC | |
by etj (Priest) on Oct 15, 2024 at 12:39 UTC | |
by ikegami (Patriarch) on Oct 15, 2024 at 15:38 UTC | |
| |
Re: Counting bytes in a Unicode document
by sectokia (Friar) on Oct 15, 2024 at 04:11 UTC | |
Re: Counting bytes in a Unicode document
by ysth (Canon) on Oct 09, 2024 at 20:35 UTC |