in reply to Unpacking byte stream long/quad little/big endian fields

I faced a similar problem while writting Net::SFTP::Foreign.

My solution was to create a set of functions which pop the data from the buffer, allowing me to write code as the following:

my $buffer = read ...; my $len = get_int32($buffer); my $cmd = get_int8($buffer); my $txt = get_string($buffer);
This is the module implementing those functions: Net::SFTP::Foreign::Buffer.

Replies are listed 'Best First'.
Re^2: Unpacking byte stream long/quad little/big endian fields
by GrandFather (Saint) on Jan 22, 2021 at 08:32 UTC

    Sure, that's essentially what I'm doing too. But sometimes people peek under the hood, and I'd like it sparking under there too.

    Tux's answer provides a nice solution for Perl >= 5.10 and that's probably what I'll run with. 5.10 has been around long enough now that I'm not much worried about supporting anything earlier.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond