View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Michael G Schwern > Tie-VecArray > Tie::VecArray



Annotate this POD

View/Report Bugs
Module Version: 0.03   Source  


  Tie::VecArray - An array interface to a bit vector.


  require Tie::VecArray;
  $vector = '';
  vec($vector, 0, 32) = 33488897;

  # Tie the vector to an array as 8 bits.
  $obj = tie @array, 'Tie::VecArray', 8, $vector;

  @array[0..3] = qw(1 255 256 -1);

  # SURPRISE!  Its 1, 255, 0, 255!
  print @array[0..3];

  # Look at the same vector as a 32 bit vector.

  # Back to 33488897
  print $array[0];


This module implements an array interface to a bit vector.


  $vec_obj = tie(@array, 'Tie::VecArray', $bits);
  $vec_obj = tie(@array, 'Tie::VecArray', $bits, $vec);

Creates a new @array tied to a bit vector. $bits is the number of bits which will be passed to vec() to interpret the vector.

If $vec is given that will be used as the bit vector, otherwise the vector will start out empty.

  $bits = $vec_obj->bits;

Get/set the bit size we'll use to interpret the vector.

When setting the bit size the length of the array might be ambiguous. (For instance, going from a one bit vector with five entries to a two bit vector... do you have two or three entries?) The length of the array will always round up. This can cause odd things to happen. Consider:

    $vec_obj = tie @vec, 'Tie::VecArray', 1;

    # A one bit vector with 5 entries.
    @vec[0..4] = (1) x 5;

    # prints a size of 5, as expected.
    print scalar @vec;

    # Switch to two bit interpretation.

    # This returns 3 since it will round up.
    print scalar @vec;

    # Switch back to one bit.

    # Whoops, 6!
    print scalar @vec;


Its slow

Due to the sluggishness of perl's tie interface, this module is about 8 times slower than using direct calls with vec(). Suck. If you care alot about speed, don't use this module. If you care about easy bit vector access or low memory usage, use this module.

No multidimentional arrays

$vec_array[$i][$j] = $num; isn't going to work. A future class will cover this possibility.


Michael G Schwern <>


"vec" in perlfunc, Tie::Array

syntax highlighting: