# $Id: BitCount.pm,v 1.6 2003/03/31 13:53:24 win Exp $ package String::BitCount; use 5.004; use strict; use Carp 'carp'; use vars qw( $VERSION @ISA @EXPORT ); require Exporter; @ISA = qw(Exporter); $VERSION = "1.13"; @EXPORT = qw(BitCount showBitCount); my $bits = '0'; for (0 .. 7) { $bits .= join '', map { ++$_ } split '', $bits; } my $test = ''; eval "require 5.006"; if (!$@) { eval "require 5.008"; if ($@) { # Perl v5.6.0 or v5.6.1 $test = 'do { use bytes; length } != do { no bytes; length }'; } else { # Perl v5.8.0 and later $test = 'tr/\0-\377/\0/c'; } $test .= ' && carp "Wide character in argument";'; } sub BitCount { my $count = 0; foreach (@_) { $count += unpack("%32b*", $_); } $count; } eval <. The arguments of showBitCount are restricted to strings of characters having code points in the range 0x00 .. 0xFF. If any string argument has code points greater than 0xFF (255) a "Wide character" warning will be issued. =head1 AUTHOR Winfried Koenig =head1 SEE ALSO perl(1) =cut