Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use common::sense;
- package Vigenere;
- use Moose;
- use autobox::Core;
- use Carp 'croak';
- use lib 'Strings';
- # upper case char
- sub toupper{ord(uc chr $_[0])}
- # accept only letters ACSII 65-90 and 97-122
- sub isalpha{chr($_[0]) =~ /[a-zA-Z]/}
- # convert string to char hex ACSII
- sub tochar{map ord, split //, $_[0]}
- has 'word' =>(
- is => 'rw',
- isa => 'Str',
- default => sub { croak "Invalid argument type 'word'";}
- );
- has 'key' =>(
- is => 'rw',
- isa => 'Str',
- default => sub { croak "Invalid argument type 'key'";}
- );
- #has 'opt' =>(
- # is => 'rw',
- # isa => 'str',
- # default => sub {'e'}
- # );
- #
- #sub op{
- # my $self = shift;
- # encode($self->word, $self->key) if ($self->opt eq 'e');
- # decode($self->word, $self->key) if ($self->opt eq 'd');
- #}
- sub encode{
- my $self = shift;
- my (@word, @key) = (tochar($self->word), tochar($self->key));
- my ($a, $tword);
- croak "'key' and 'word' are diferent lenght\n"
- unless ($#word == $#key);
- for($a = 0; $a <= @word; $a++){
- if (isalpha($word[$a]), isalpha($key[$a])){
- $word[$a] = toupper($word[$a]) - 65;
- $key[$a] = toupper($key[$a]);
- my $cript = $key[$a] + $word[$a];
- $cript -= 26 if ($cript >= 91);
- $word[$a] = $cript;
- $tword .= chr $word[$a];
- }
- }
- return $tword;
- }
- sub decode{
- my $self = shift;
- my (@word, @key) = (tochar($self->word), tochar($self->key));
- my ($a, $tword);
- croak "'key' and 'word' are diferent lenght\n"
- unless ($#word == $#key);
- for($a = 0; $a <= @word; $a++){
- if (isalpha($word[$a]), isalpha($key[$a])){
- $word[$a] = toupper($word[$a]);
- $key[$a] = toupper($key[$a]);
- my $cript = $word[$a] - $key[$a] + 65;
- $cript += 26 if ($cript <= 64);
- $word[$a] = $cript;
- $tword .= chr $word[$a];
- }
- }
- return $tword;
- }
- 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement