Data::BitStream::Code::Escape - A Role implementing Escape codes


version 0.08


A role written for Data::BitStream that provides get and set methods for Escape codes. The role applies to a stream object.

An Escape code is a code where a binary value is read using m bits (m >= 1), and if the read value is equal to 2^m-1, then another n bits is read (n >= 1), etc. They are somewhat similar to Start/Stop codes, though use an escape value inside each block instead of a unary indicator. For example a 3-7 code would look like:

  0     000
  1     001
  2     010
  6     110
  7     1110000000
  8     1110000001
  134   1111111111

These codes are not uncommon in a variety of applications where extremely simple variable length coding is desired, but for various reasons none of the more sophisticated methods are used. These codes can be quite useful for some cases such as a 8-32 code which encodes 0-254 in one byte and values greater than 254 in five bytes. Based on the frequencies and implementation, this may be more desirable than other methods such as a startstop(7-25) code which encodes 0-127 in one byte and values greater than 127 in four bytes.

For many cases, and almost all where more than two parameters are used, Start/Stop codes will be more space efficient.


  use Data::BitStream;
  my $stream = Data::BitStream->new;
  my @array = (4, 2, 0, 3, 7, 72, 0, 1, 13);

  $stream->put_escape( [3,7], @array );
  my @array2 = $stream->get_escape( [3,7], -1);

  # @array equals @array2


Provided Object Methods

put_escape([@m], $value)
put_escape([@m], @values)

Insert one or more values as Escape codes. Returns 1.

get_escape([@m], $count)

Decode one or more Escape codes from the stream. If count is omitted, one value will be read. If count is negative, values will be read until the end of the stream is reached. In scalar context it returns the last code read; in array context it returns an array of all codes read.


The Escape parameters are passed as a array reference.

There must be at least one parameter. Each parameter must be greater than or equal to zero. Each value is the number of bits in the block.

Required Methods


These methods are required for the role.




Dana Jacobsen <>


Copyright 2011 by Dana Jacobsen <>

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.