Advertisement
Guest User

Vigenere.pm by Aureliano Guedes

a guest
Nov 11th, 2013
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.04 KB | None | 0 0
  1. use common::sense;
  2.  
  3. package Vigenere;
  4.  
  5. use Moose;
  6. use autobox::Core;
  7. use Carp 'croak';
  8. use lib 'Strings';
  9.  
  10. sub BUILD{
  11.     my $self = shift;
  12.     my (@w, @k);
  13.     @w = split (//, $self->word);
  14.     @k = split (//, $self->key);
  15.     croak "'key' and 'word' are diferent lenght\n"
  16.         if($#w != $#k);
  17. }
  18.  
  19. has 'word' => (
  20.         is => 'rw',
  21.         isa => 'Str',
  22.         required => 1,
  23.         );
  24. has 'key' => (
  25.         is => 'rw',
  26.         isa => 'Str',
  27.         required => 1,
  28.         );
  29. has 'op' => (
  30.         is => 'rw',
  31.         isa => 'Str',
  32.         required => 1,
  33.         );
  34.  
  35. sub gen{
  36.     my $self = shift;
  37.     my $opt = $self->op;
  38.     my @word = split //, $self->word;
  39.     my @key = split //, $self->key;
  40.     my ($a, $tword);   
  41.     for ($a = 0; $a < @word; $a++){
  42.         if(($word[$a] =~ /[a-zA-Z]/) and ($key[$a] =~ /[a-zA-Z]/)){
  43.             $word[$a] = ord(uc($word[$a]));
  44.             $key[$a] = ord(uc($key[$a]));
  45.             if ($opt eq 'e'){$tword .= chr ((($word[$a] + $key[$a])%26) + 65)}
  46.             if ($opt eq 'd'){$tword .= chr ((($word[$a] - $key[$a])%26) + 65)}
  47.         }
  48.         else{ croak "'word' and 'key' need to be only letter"}
  49.     }
  50.     return $tword;
  51. }
  52.  
  53. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement