in reply to Re: Can't decode ill-formed UTF-8 octet sequence
in thread Can't decode ill-formed UTF-8 octet sequence
The above is incorrect, as it "decodes" the torrent file too. You want binary mode (:raw) for that, and an encoding layer (:encoding(UTF-8)) on the output.
Unfortunately, while we can set a default encoding layer for the files read via ARGV, there's no way to make it use binary mode. It gives us a mess.
perl -e' use v5.36; use utf8::all; use Bencode qw( bdecode ); use File::Slurper qw( read_binary ); binmode STDIN; sub process_torrent { say bdecode( $_[0] )->{info}{name}; } if ( @ARGV ) { process_torrent( read_binary( $_ ) ) for @ARGV; } else { process_torrent( do { local $/; <STDIN> } ); } '
Outside of Windows, you can probably get away with not using binary mode.
perl -gne' use v5.36; use Bencode qw( bdecode ); binmode STDOUT, ":encoding( UTF-8 )"; binmode STDERR, ":encoding( UTF-8 )"; say bdecode( $_ )->{info}{name}; '
|
|---|