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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.