open(FILE, shift) or die $!; my (@buffer, @bits); while(!eof(FILE)) { if (@buffer == 0) { #if it's empty, fill it my $bytes; #read 30 bytes at a time read(FILE, $bytes, 30); my @ary = map {ord} split("", $bytes); foreach my $byte (@ary) { push @buffer, split("", unpack("B*", $byte)); } } while(@bits < 128) { push (@bits, shift(@buffer)); last if (@buffer == 0); } #the first couple of reads will not fill the bits array next if (@bits < 128); #your processing on @bits goes here shift @bits; }