in reply to Techniques On Saving Memory

I'm not sure what you mean by "reference", but pack/unpack do contain support for two kinds of templates, "P" and "p", that can be used to reference/dereference some block of memory. So if what you want is a (zero-terminated) string or a packed, fixed length structure, you can use one of these. You must be really careful not to move your strings containing the packed data around, or you'll end up dereferencing the wrong memory block. So no changing the length of the scalar holding that block, once you've started packing.

I thought of using substr to point inside a string, but it doesn't work; but the following gives some form of workaround:

$x = "one\0two\0"; $b = pack "p", $x; # pointer to "one" $b .= pack 'I', 4 + unpack 'I', $b; # pointer to "two" = substr($x, 4) printf "length: %i\n", length $b; printf "%i %i\n", unpack "II", $b; printf "%s\n", $_ for unpack "pp", $b;
Result:
length: 8
2279940 2279944
one
two
Like I said, $x contains the packed data, so don't move that around.

For unpacking a fixed length string, you can use "P12" for a 12 byte string, for example. Appending the following snippet to the above code, I get the following additional result:

printf "%s\n", $_ for unpack "P2P2", $b;
on
tw

Note: the unpacked data is a copy of the data, not a reference into the same, original data.