How to create and use a byte array in perl?

$var = pack "C2", 0x20, 0x30;

seems to work quite well, but now, how can I access the elements of the array? Make changes to items? In place, if possible. The semantics of pack / unpack are not very friendly.

I am currently using substr($var, $index, 1, substr($var, $index, 1) + 10) to add 10 to the elements in place.

And for integrators, if I need a 100-byte 0x20 array, what's the best way? $var = "\x20" x 100 works, is this the "right" way?

+4
source share
2 answers

two questions, two answers:

Q. seems to work quite well, but now how can I access array elements?

a. vec () is your friend:

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

will do what you want.

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

a. this is normal in my book.

+8
source

Does this fit your needs? Tie :: Array :: Pack

+2
source

All Articles