Perl 6是否具有Python的bytearray方法的等价物?

我在Perl 6 doc中找不到像Python一样的bytearray方法或类似方法.在Python中,bytearray定义如下:

class bytearray([source[, encoding[, errors]]])

Return a new array of bytes. The bytearray class is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods that the str type has, see String Methods.

Perl 6应该提供这种方法还是某些模块?

brian d foy answer基本上是正确的.你可以将this code翻译成Perl6

 my $frame = Buf.new; 
 $frame.append(0xA2); 
 $frame.append(0x01); 
 say $frame; # OUTPUT: «Buf:0x<a2 01>␤»

但是,声明不一样:

bu = bytearray( 'þor', encoding='utf8',errors='replace')

在Python中与Perl 6中的相同

my $bú =  Buf.new('þor'.encode('utf-8')); 
say $bú; # OUTPUT: «Buf:0x<c3 be 6f 72>␤» 

并且使用与错误转换等效的东西,由于Perl 6接近Unicode规范化的方式,该方法是不同的;你可能不得不使用UTF8 Clean 8编码.

然而,对于大多数用途,我认为Buf,正如brian d foy所指出的那样,是正确的.

翻译自:https://stackoverflow.com/questions/51009154/does-perl-6-have-an-equivalent-to-pythons-bytearray-method

转载注明原文:Perl 6是否具有Python的bytearray方法的等价物?