How do I create and use a byte array in perl?

arrays, perl

Solution

two questions, two answers:

Q. seems to work well enough, but now how do I address the elements of the array?

A. vec() is your friend:

vec($var, $index, 8) += 10;

will do what you want.

Q. for intializers, if I need a 100 byte array of 0x20, what's the best way? $var = "\x20" x 100 works, is it the 'right' way?

A. it's OK in my book.

Problem

`$var = pack "C2", 0x20, 0x30;` seems to work well enough, but now how do I address the elements of the array? Make changes to elements? In-place if possible. The pack/unpack semantics are not very friendly. Currently I'm using `substr($var, $index, 1, substr($var, $index, 1) + 10)` to add 10 to elements in-place. And for intializers, if I need a 100 byte array of 0x20, what's the best way? `$var = "\x20" x 100` works, is it the 'right' way?

Original source