Does Perl 6 have an equivalent to Python's bytearray method?
I think you're looking for Buf - a mutable sequence of (usually unsigned) integers. Opening a file with :bin
returns a Buf
.
brian d foy answer is essentially correct. You can pretty much translate this code into Perl6
my $frame = Buf.new;
$frame.append(0xA2);
$frame.append(0x01);
say $frame; # OUTPUT: «Buf:0x<a2 01>»
However, the declaration is not the same:
bu = bytearray( 'þor', encoding='utf8',errors='replace')
in Python would be equivalent to this in Perl 6
my $bú = Buf.new('þor'.encode('utf-8'));
say $bú; # OUTPUT: «Buf:0x<c3 be 6f 72>»
And to use something equivalent to the error transformation, the approach is different due to the way Perl 6 approaches Unicode normalization; you would probably have to use UTF8 Clean 8
encoding.
For most uses, however, I guess Buf
, as indicated by brian d foy, is correct.