File Coverage

blib/lib/Crypt/Role/ScrambledAlphabet.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 28 28 100.0


line stmt bran cond sub pod time code
1 1     1   3911 use 5.008;
  1         3  
  1         33  
2 1     1   5 use strict;
  1         2  
  1         22  
3 1     1   5 use warnings;
  1         1  
  1         48  
4              
5             package Crypt::Role::ScrambledAlphabet;
6              
7             our $AUTHORITY = 'cpan:TOBYINK';
8             our $VERSION = '0.003';
9              
10 1     1   4 use Moo::Role;
  1         2  
  1         7  
11 1     1   347 use Const::Fast;
  1         2  
  1         9  
12 1     1   908 use Types::Common::String qw(Password);
  1         43066  
  1         14  
13 1     1   535 use namespace::sweep;
  1         3  
  1         9  
14              
15             has password => (
16             is => 'lazy',
17             isa => Password,
18             required => !!1,
19             );
20              
21             around alphabet => sub
22             {
23             my $next = shift;
24             my $self = shift;
25             my $orig = $self->$next(@_);
26            
27             my %allowed_letters;
28             $allowed_letters{$_}++ for @$orig;
29            
30             my @letters = grep $allowed_letters{$_}, split '', $self->password;
31             push @letters, @$orig;
32            
33             my %seen;
34             return [ grep !$seen{$_}++, @letters ];
35             };
36              
37             1;
38              
39             __END__
40              
41             =pod
42              
43             =encoding utf-8
44              
45             =head1 NAME
46              
47             Crypt::Role::ScrambledAlphabet - use a password to change the order of the alphabet
48              
49             =head1 SYNOPSIS
50              
51             use Crypt::Polybius;
52            
53             my $square = Crypt::Polybius->new_with_traits(
54             traits => ['Crypt::Role::ScrambledAlphabet'],
55             password => "FISHING",
56             );
57            
58             print $square->encipher("Hello world!"), "\n";
59              
60             =head1 DESCRIPTION
61              
62             This role scrambles an alphabet. For example, given the password
63             "FISHING", the alphabet from L<Crypt::Role::LatinAlphabet> are
64             rearranged as follows:
65              
66             A B C D E F G H I K L M N O P Q R S T U V W X Y Z
67             F I S H N G A B C D E K L M O P Q R T U V W X Y Z
68              
69             =head2 Attrbutes
70              
71             =over
72              
73             =item C<< password >>
74              
75             The password to scramble the alphabet with. Should use at least some
76             letters which exist in the available alphabet (case-sensitive!).
77              
78             This attribute is required, and must conform to the C<Password> type
79             constraint (L<Types::Common::String>).
80              
81             =back
82              
83             =head2 Method Modifiers
84              
85             =over
86              
87             =item C<< alphabet >>
88              
89             The alphabet method is wrapped, changing the order of the letters.
90              
91             =back
92              
93             =head1 BUGS
94              
95             Please report any bugs to
96             L<http://rt.cpan.org/Dist/Display.html?Queue=Crypt-Polybius>.
97              
98             =head1 SEE ALSO
99              
100             L<Crypt::Polybius>,
101             L<Crypt::Polybius::Greek>.
102              
103             =head1 AUTHOR
104              
105             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
106              
107             =head1 COPYRIGHT AND LICENCE
108              
109             This software is copyright (c) 2014 by Toby Inkster.
110              
111             This is free software; you can redistribute it and/or modify it under
112             the same terms as the Perl 5 programming language system itself.
113              
114             =head1 DISCLAIMER OF WARRANTIES
115              
116             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
117             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
118             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
119