String(3)
π¦ NAME π¦
Okay bestie, this is the String(3) manual page from the User Contributed Perl Documentation, and itβs all about Unicode::String β String of Unicode characters (UTF-16BE). Iconic. Obsessed. No cap. π
β¨ SYNOPSIS β¨
use Unicode::String qw(utf8 latin1 utf16be);
$u = utf8("string");
$u = latin1("string");
$u = utf16be("\0s\0t\0r\0i\0n\0g");
print $u->utf32be; # 4 byte characters
print $u->utf16le; # 2 byte characters + surrogates
print $u->utf8; # 1-4 byte characters
πΈ DESCRIPTION πΈ
A Unicode::String object represents a sequence of Unicode characters. Methods are provided to convert between various external formats (encodings) and Unicode::String objects, and methods are provided for common string manipulations. Itβs giving practical and pretty.
The functions utf32be(), utf32le(), utf16be(), utf16le(), utf8(), utf7(), latin1(), uhex(), uchr() can be imported from the Unicode::String module and will work as constructors initializing strings of the corresponding encoding. Slay.
The Unicode::String objects overload various operators, which means that they in most cases can be treated like plain strings. Lowkey convenient.
Internally a Unicode::String object is represented by a string of 2 byte numbers in network byte order (big-endian). This representation is not visible by the API provided, but it might be useful to know in order to predict the efficiency of the provided methods. iykyk.
π METHODS π
β¨ Class methods β¨
The following class methods are available:
Unicode::String->stringify_as,Unicode::String->stringify_as( $enc )β This method is used to specify which encoding will be used whenUnicode::Stringobjects are implicitly converted to and from plain strings.
If an argument is provided it sets the current encoding. The argument should have one of the following: ucs4, utf32, utf32be, utf32le, ucs2, utf16, utf16be, utf16le, utf8, utf7, latin1 or hex. The default is utf8.
The stringify_as() method returns a reference to the current encoding function.
$us = Unicode::String->new,$us = Unicode::String->new( $initial_value )β This is the object constructor. Without argument, it creates an emptyUnicode::Stringobject. If an$initial_valueargument is given, it is decoded according to the specifiedstringify_as()encoding, UTF-8 by default.
In general it is recommended to import and use one of the encoding specific constructor functions instead of invoking this method.
π Encoding methods π
These methods get or set the value of the Unicode::String object by passing strings in the corresponding encoding. If a new value is passed as argument it will set the value of the Unicode::String, and the previous value is returned. If no argument is passed then the current value is returned. Vibe check: pass a new value, get the old one back.
To illustrate the encodings we show how the 2 character sample string of "Β΅m" (micro meter) is encoded for each one. Encoding reveal! β¨
$us->utf32be,$us->utf32be( $newval )β The string passed should be in the UTF-32 encoding with bytes in big endian order. The sample"Β΅m"is"\0\0\0\xB5\0\0\0m"in this encoding.
Alternative names for this method are utf32() and ucs4().
$us->utf32le,$us->utf32le( $newval )β The string passed should be in the UTF-32 encoding with bytes in little endian order. The sample"Β΅m"is"\xB5\0\0\0m\0\0\0"in this encoding.$us->utf16be,$us->utf16be( $newval )β The string passed should be in the UTF-16 encoding with bytes in big endian order. The sample"Β΅m"is"\0\xB5\0m"in this encoding.
Alternative names for this method are utf16() and ucs2().
If the string passed to utf16be() starts with the Unicode byte order mark in little endian order, the result is as if utf16le() was called instead.
$us->utf16le,$us->utf16le( $newval )β The string passed should be in the UTF-16 encoding with bytes in little endian order. The sample"Β΅m"is"\xB5\0m\0"in this encoding. This is the encoding used by the Microsoft Windows API.
If the string passed to utf16le() starts with the Unicode byte order mark in big endian order, the result is as if utf16le() was called instead.
$us->utf8,$us->utf8( $newval )β The string passed should be in the UTF-8 encoding. The sample"Β΅m"is"\xC2\xB5m"in this encoding.$us->utf7,$us->utf7( $newval )β The string passed should be in the UTF-7 encoding. The sample"Β΅m"is"+ALU-m"in this encoding.
The UTF-7 encoding only use plain US-ASCII characters for the encoding. This makes it safe for transport through 8-bit stripping protocols. Characters outside the US-ASCII range are base64-encoded and '+' is used as an escape character. The UTF-7 encoding is described in RFC 1642.
If the (global) variable $Unicode::String::UTF7_OPTIONAL_DIRECT_CHARS is TRUE, then a wider range of characters are encoded as themselves. It is even TRUE by default. The characters affected by this are:
! " # $ % & * ; < = > @ [ ] ^ _ ` { | }
$us->latin1,$us->latin1( $newval )β The string passed should be in the ISO-8859-1 encoding. The sample"Β΅m"is"\xB5m"in this encoding.
Heads up, bestie: characters outside the"\x00" .. "\xFF"range are simply removed from the return value of thelatin1()method. If you want more control over the mapping from Unicode to ISO-8859-1, use theUnicode::Map8class. This is also the way to deal with other 8-bit character sets.
$us->hex,$us->hex( $newval )β The string passed should be plain ASCII where each Unicode character is represented by the"U+XXXX"string and separated by a single space character. The"U+"prefix is optional when setting the value. The sample"Β΅m"is"U+00b5 U+006d"in this encoding.
π String Operations π
The following methods are available:
$us->as_stringβ Converts aUnicode::Stringto a plain string according to the setting ofstringify_as(). The defaultstringify_as()encoding isutf8.$us->as_numβ Converts aUnicode::Stringto a number. Currently only the digits in the range0x30 .. 0x39are recognized. The plan is to eventually support all Unicode digit characters.$us->as_boolβ Converts aUnicode::Stringto a boolean value. Only the empty string is FALSE. A string consisting of only the characterU+0030is considered TRUE, even if Perl consider"0"to be FALSE.$us->repeat( $count )β Returns a newUnicode::Stringwhere the content of$usis repeated$counttimes. This operation is also overloaded as:
$us x $count
$us->concat( $other_string )β Concatenates the string$usand the string$other_string. If$other_stringis not anUnicode::Stringobject, then it is first passed to theUnicode::String->newconstructor function. This operation is also overloaded as:
$us . $other_string
$us->append( $other_string )β Appends the string$other_stringto the value of$us. If$other_stringis not anUnicode::Stringobject, then it is first passed to theUnicode::String->newconstructor function. This operation is also overloaded as:
$us .= $other_string
$us->copyβ Returns a copy of the currentUnicode::Stringobject. This operation is overloaded as the assignment operator.$us->lengthβ Returns the length of theUnicode::String. Surrogate pairs are still counted as 2.$us->byteswapβ This method will swap the bytes in the internal representation of theUnicode::Stringobject.
Unicode reserve the character U+FEFF character as a byte order mark. This works because the swapped character, U+FFFE, is reserved to not be valid. For strings that have the byte order mark as the first character, we can guaranty to get the byte order right with the following code:
$ustr->byteswap if $ustr->ord == 0xFFFE;
$us->unpackβ Returns a list of integers each representing an UCS-2 character code.$us->pack( @uchr )β Sets the value of$usas a sequence of UCS-2 characters with the characters codes given as parameter.$us->ordβ Returns the character code of the first character in$us. Theord()method deals with surrogate pairs, which gives us a result-range of0x0 .. 0x10FFFF. If the$usstring is empty, undef is returned.$us->chr( $code )β Sets the value of$usto be a string containing the character assigned code$code. The argument$codemust be an integer in the range0x0 .. 0x10FFFF. If the code is greater than0xFFFFthen a surrogate pair created.$us->nameβ In scalar context returns the official Unicode name of the first character in$us. In array context returns the name of all characters in$us. Also seeUnicode::CharName.$us->substr( $offset ),$us->substr( $offset, $length ),$us->substr( $offset, $length, $subst )β Returns a sub-string of$us. Works similar to the builtinsubstr()function.$us->index( $other ),$us->index( $other, $pos )β Locates the position of$otherwithin$us, possibly starting the search at position$pos.$us->chopβ Chops off the last character of$usand returns it (as aUnicode::Stringobject).
π₯ FUNCTIONS π₯
The following functions are provided. None of these are exported by default.
byteswap2( $str, ... )β This function will swap 2 and 2 bytes in the strings passed as arguments. If this function is called in void context, then it will modify its arguments in-place. Otherwise, the swapped strings are returned.byteswap4( $str, ... )β Thebyteswap4function works similar tobyteswap2, but will reverse the order of 4 and 4 bytes.latin1( $str ),utf7( $str ),utf8( $str ),utf16le( $str ),utf16be( $str ),utf32le( $str ),utf32be( $str )β Constructor functions for the various Unicode encodings. These return newUnicode::Stringobjects. The provided argument should be encoded correspondingly.uhex( $str )β Constructs a newUnicode::Stringobject from a string of hex values. Seehex()method above for description of the format.uchar( $num )β Constructs a new one characterUnicode::Stringobject from a Unicode character code. This works similar to perl's builtinchr()function.
And yes, bestie, the DESCRIPTION up top says uchr() while this function list says uchar() β both are literally in the page, so Iβm not touching them. iykyk. π
π SEE ALSO π
Unicode::CharName,Unicode::Map8<http://www.unicode.org/>perlunicode
π COPYRIGHT π
Copyright 1997-2000,2005 Gisle Aas.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
π POD ERRORS π
Okay bestie, so plot twist β the doc you just read? It had some coding oopsies in it, and this section is here to explain them. In the original's own words: "Hey! The above document had some coding errors, which are explained below:" π«Ά
Here's the tea: π
- Around line 600:
Non-ASCII character seen before =encoding in '"Β΅m"'. Assuming CP1252
So yeah β somewhere down at line 600, there's a "Β΅m" chilling out before any =encoding line shows up, so the whole thing just gets assumed to be CP1252 and everybody moves on with their lives. Lowkey iconic of it to just guess, honestly. π
And that's the whole vibe check, bestie β you made it to the end! π This page is from perl version v5.42.0, dated 2016-09-09, and it's the String(3) page. Go forth and slay your string handling, no cap. β¨ππ
π¦ where this page comes from
- opensuse-leap 16.0: perl-Unicode-String 2.10-160000.2.2 Β· GPL-1.0+ or Artistic-1.0
- opensuse-leap 16.0: perl-Unicode-String 2.10-160000.2.2 Β· GPL-1.0+ or Artistic-1.0
- opensuse-leap 16.0: perl-Unicode-String 2.10-160000.2.2 Β· GPL-1.0+ or Artistic-1.0
- opensuse-leap 16.0: perl-Unicode-String 2.10-160000.2.2 Β· GPL-1.0+ or Artistic-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms