tags:

views:

800

answers:

2

$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?

+2  A: 

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

Nifle
From the perldoc on cpan: "Since this module has to pack() for each STORE and unpack() for each FETCH, it is much slower than the native array", so I'm not sure that's much better, even if it is much friendlier than naked pack/unpack.
davenpcj
+6  A: 

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.

Massa