From 556f1e542cdeadb7dc2f549ec7915fe28f5bb112 Mon Sep 17 00:00:00 2001 From: dakkar Date: Mon, 29 Aug 2011 13:06:32 +0100 Subject: static rotors --- lib/Enigmatic/Rotor.pm | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 lib/Enigmatic/Rotor.pm (limited to 'lib/Enigmatic/Rotor.pm') diff --git a/lib/Enigmatic/Rotor.pm b/lib/Enigmatic/Rotor.pm new file mode 100644 index 0000000..a600b43 --- /dev/null +++ b/lib/Enigmatic/Rotor.pm @@ -0,0 +1,50 @@ +package Enigmatic::Rotor; +use DAKKAR::p 'class'; +use Enigmatic::Types qw(WiringMap Letter RotorPos); + +has wiring => ( + is => 'ro', + isa => WiringMap, + coerce => 1, +); + +has ring_setting => ( + is => 'rw', + isa => RotorPos, + default => 0, +); + +around BUILDARGS => sub { + my $orig = shift; + my $class = shift; + + if ( @_ == 1 && !ref $_[0] ) { + return $class->$orig( wiring => $_[0] ); + } + else { + return $class->$orig(@_); + } +}; + +sub map { + my $self = shift; + my ($letter) = pos_validated_list( + \@_, + { isa => Letter }, + ); + + return $self->wiring->at($self->_apply_ring_setting($letter)); +} + +sub _apply_ring_setting { + my $self = shift; + my ($letter) = pos_validated_list( + \@_, + { isa => Letter }, + ); + + return chr( + (ord($letter) - ord('A') + $self->ring_setting) + % 26 + +ord('A') ); +} -- cgit v1.2.3