Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does encode delete the argument?

Tags:

encode

perl

Why does encode delete the passed argument, if CHECK is set to a true value?

#!/usr/bin/env perl
use warnings;
use strict;
use utf8;
use Encode;

my $decoded = 'h';
if ( eval { encode( 'utf-8', $decoded, 1 ); 1 } ) {
    print "|$decoded|\n";    # prints ||
}
like image 813
sid_com Avatar asked Apr 24 '15 08:04

sid_com


People also ask

What is the use of encode () method?

The encode () method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used. Optional. A String specifying the encoding to use.

What if no encoding is specified in the string?

If no encoding is specified, UTF-8 will be used. Optional. A String specifying the encoding to use. Default is UTF-8 Optional.

How to encode a string in Python?

Python String encode () Method 1 Definition and Usage. The encode () method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used. 2 Syntax 3 Parameter Values. A String specifying the encoding to use. A String specifying the error method. 4 More Examples

Why are there encoding errors in my files?

The file is checked into source control in an encoding that is different from what VS Code or PowerShell expects. This can happen when collaborators use editors with different encoding configurations. Often encoding errors present themselves as parse errors in scripts.


1 Answers

It is for use in the case where you are repeatedly passing chunks of data to encode or decode. The idea is that the function will remove the part of the string that it has translated, and you need only append the next chunk to what is left. It is useful for handling multi-byte encodings that may be split across two chunks.

If you don't want this behaviour then you can OR the Encode::LEAVE_SRC bit into the third parameter. Like this

use utf8;
use strict;
use warnings;

use Encode qw/ encode decode FB_CROAK LEAVE_SRC /;
use Data::Dump;

my $decoded = 'ABC';
dd $decoded;
my $encoded = encode( 'UTF-8', $decoded, FB_CROAK | LEAVE_SRC );
dd $decoded;
dd $encoded;

output

"ABC"
"ABC"
"ABC"
like image 124
Borodin Avatar answered Dec 02 '22 15:12

Borodin