in reply to Perl strings questions
my $unicode_string = "αβγαabc123";
# unicode chars mixed with lower-ascii
This is not a unicode string. (choroba notes below that it originally was, but PerlMonks mangles these within code blocks) It is a HTML encoding of a unicode string in plain ASCII. To get a unicode string from this, do:
use HTML::Entities; my $html_string = "αβγαabc123"; my $unicode_string = decode_entities($html_string);
2) Also, I have a question about why I need to encode in "UTF-8". Does that make sure that the "double-bytes" and possible "single-bytes" are all becoming a stream of "single-bytes"?
After UTF-8 encoding, you end up with a stream of single-byte characters.
However, you do not strictly need to encode in UTF-8. The text encoding is an agreement between the sender and the receiver, this can either be done by explicit specification (example: Content-Type: text/html; encoding=utf8), by some standard or defaults (examples: XML defaults to UTF-8, HTML defaults to ISO-8859-1), or just by the developers talking to each other over a beer (not recommended). These days, UTF-8 is highly recommended because it is able to represent any unicode character in a consistent way.
I am trying to find the safe way to do things when strings are mixed,My recommendation: Just don't do that. Text and binary data don't mix well in a simple string. Finding the borders is hard to do in a safe way since binary data may occasionally look like text.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl strings questions
by choroba (Cardinal) on Jun 02, 2021 at 11:10 UTC | |
by haj (Vicar) on Jun 02, 2021 at 11:23 UTC | |
|
Re^2: Perl strings questions
by bliako (Abbot) on Jun 02, 2021 at 12:29 UTC | |
by haj (Vicar) on Jun 02, 2021 at 14:25 UTC | |
by bliako (Abbot) on Jun 02, 2021 at 17:32 UTC |