Acme-Mobile-Therbligs
view release on metacpan or search on metacpan
lib/Acme/Mobile/Therbligs.pm view on Meta::CPAN
use Exporter;
our @ISA = qw( Exporter );
our @EXPORT = qw( count_therbligs );
our %EXPORT_TAGS = (
'all' => [ @EXPORT ],
);
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
use constant DEFAULT_SAME_KEY => 1;
use Carp;
use YAML qw( Load Dump );
=item new
$obj = Acme::Mobile::Therbligs->new();
This is used for the object-oriented interface. It is only useful if
you want to specify your own keypad or modify the rules:
open $fh, 'mykeypad.yml';
$obj = Acme::Mobile::Therbligs->new($fh, \%rules );
The rule file is in L<YAML> format that specifies the characters for
each key pressed (in the order that they occur for each key press).
The optional rules allow one to change the behavior of the counting
function:
$obj = Acme::Mobile::Therbligs->new($fh,
{
SAME_KEY => 1,
NO_SENTENCE_CAPS => 0,
});
=over
=item SAME_KEY
The number of therbligs to count as waiting when having to enter
letters which require pressing the same key (as with the word "high").
Defaults to C<1>.
=item NO_SENTENCE_CAPS
By default the initial letter of the message and of each sentence is
assumed to be capitalized (when counting in case-sensitive mode). This
option disabled that.
=back
=cut
sub new {
my $class = shift;
my $self = { };
bless $self, $class;
$self->_initialize(@_);
return $self;
}
my $Default;
sub _initialize {
my $self = shift;
my $fh = shift;
my $rule = shift || { };
$self->{SAME_KEY} = $rule->{SAME_KEY} || DEFAULT_SAME_KEY;
$self->{NO_SENTENCE_CAPS} = $rule->{NO_SENTENCE_CAPS} || 0;
$self->{NO_SHIFT_CAPS} = $rule->{NO_SHIFT_CAPS} || 0;
unless (defined $Default) {
$Default = join("", <DATA>, "\n");
}
my $file = (defined $fh) ? join("", <$fh>, "\n") : $Default;
my $keys = Load($file);
$self->{KEYPAD} = $keys;
foreach my $key (0..9) {
croak "Missing $key key",
unless (exists $keys->{$key});
}
$self->{CHAR} = { };
foreach my $key (keys %$keys) {
my $thurb = 1;
foreach my $char (split //, $keys->{$key}) {
$self->{CHAR}->{$char} = [$key, $thurb++];
}
}
return $self;
}
my $Self;
{
$Self = __PACKAGE__->new(undef, {
SAME_KEY => DEFAULT_SAME_KEY,
NO_SENTENCE_CAPS => 0,
NO_SHIFT_CAPS => 0,
});
}
=item count_therbligs
$count = count_therbligs($message, $case_flag);
$count = $obj->count_therbligs($message, $case_flag);
Returns the number of "therbligs" (keystrokes) used to generate the
message. A therblig is either a keystroke, or the pause when one has
to wait in order to enter multiple letters from the same key (such as
( run in 2.281 seconds using v1.01-cache-2.11-cpan-a49fcb8fa48 )