File Coverage

blib/lib/PerlIO/via/Rotate.pm
Criterion Covered Total %
statement 83 131 63.3
branch 16 66 24.2
condition 4 9 44.4
subroutine 26 30 86.6
pod 0 25 0.0
total 129 261 49.4


line stmt bran cond sub pod time code
1             package PerlIO::via::Rotate;
2              
3             $VERSION= '0.08';
4              
5             # be strict and do everything on octets
6 6     6   3292 use strict;
  6         11  
  6         149  
7 6     6   5330 use bytes;
  6         40  
  6         26  
8              
9             # initialize the base rotational strings
10             my @rotate= ( '', qw(
11             b-za
12             c-zab
13             d-za-c
14             e-za-d
15             f-za-e
16             g-za-f
17             h-za-g
18             i-za-h
19             j-za-i
20             k-za-j
21             l-za-k
22             m-za-l
23             n-za-m
24             o-za-n
25             p-za-o
26             q-za-p
27             r-za-q
28             s-za-r
29             t-za-s
30             u-za-t
31             v-za-u
32             w-za-v
33             x-za-w
34             yza-x
35             za-y
36             ), '' );
37              
38             # satisfy -require-
39             1;
40              
41             #-------------------------------------------------------------------------------
42             #
43             # Standard Perl features
44             #
45             #-------------------------------------------------------------------------------
46             # IN: 1 class to bless with
47             # 2..N parameters passed in -use-
48              
49             sub import {
50 6     6   34 shift;
51              
52             # set up defaults
53 6 100 66     36 @_= 0..26 if @_ == 1 and $_[0] eq ':all';
54 6 100       27 @_= 13 if !@_;
55              
56             # process all rotations
57 6         132 my @huh;
58             ROTATION:
59 6         14 foreach (@_) {
60              
61             # huh?
62 34 50 33     767 push( @huh, "Invalid rotational value: $_" ), next ROTATION
      33        
63             if !m#^[0-9]+$# or $_ < 0 or $_ > 26;
64              
65             # we've already done this one
66 34         82 my $module= "PerlIO/via/rot$_.pm";
67 34 50       134 next ROTATION if $INC{$module};
68              
69             # source for the module
70 34         541 my $source= <<"SRC";
71             package PerlIO::via::rot$_;
72             use bytes;
73             \@PerlIO::via::rot$_\::ISA= 'PerlIO::via::Rotate';
74             \$PerlIO::via::rot$_\::VERSION= '$PerlIO::via::Rotate::VERSION';
75             SRC
76              
77             # we can do encoding for this
78 34 100       119 if ( my $encode= $rotate[$_] . uc( $rotate[$_] ) ) {
79 32         61 my $other= 26 - $_;
80 32         61 my $decode= $rotate[$other] . uc( $rotate[$other] );
81              
82             # add the source code for this rotation (PUSHED is inherited)
83 32         96 $source .= <<"SRC";
84             sub FILL {
85             local \$_= readline( \$_[1] );
86             return if !defined \$_;
87             tr/a-zA-Z/$decode/;
88             return \$_;
89             } #FILL
90             sub WRITE {
91             local \$_= \$_[1];
92             tr/a-zA-Z/$encode/;
93             return ( print { \$_[2] } \$_ ) ? length() : -1;
94             } #WRITE
95             SRC
96             }
97              
98             # make the module available and mark as loaded
99 6 50   6 0 31 if ( eval "$source; 1" ) {
  6 100   28 0 12  
  6 50   4 0 22  
  34 0   1 0 3860  
  28 0   1 0 95  
  28 0   1 0 78  
  25 0   1 0 40  
  25 0   1 0 84  
  4 0   1 0 38  
  4 0   10 0 19  
  4 100   1 0 16  
  4 0   2 0 59  
  1 50   1 0 131  
  1 0   1 0 5  
  1 0   1 0 2  
  1 0   1 0 4  
  1 0   1 0 5  
  1 0   1 0 2  
  1 0   1 0 3  
  1 0   0 0 5  
  1 0   0 0 1  
  1 0   0 0 4  
  1 0   0   5  
  1 0       2  
  1         3  
  1         6  
  1         2  
  1         5  
  1         6  
  1         2  
  1         8  
  1         5  
  1         2  
  1         4  
  1         9  
  1         3  
  1         3  
  1         5  
  1         2  
  1         3  
  1         5  
  1         2  
  1         4  
  10         31  
  10         32  
  9         14  
  8         20  
  0         0  
  0         0  
  0         0  
  0         0  
  1         3  
  1         6  
  1         3  
  1         14  
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
100 34         870 $INC{$module}= $INC{'PerlIO/via/Rotate.pm'};
101             }
102              
103             # huh?
104             else {
105 1         5 push @huh, "Could not create module for $_: $@";
106             }
107             }
108              
109             # sorry, can't go on
110 6 50       21 die join "\n", "These errors were found:", @huh if @huh;
111              
112 6         129 return;
113             } #import
114              
115             #-------------------------------------------------------------------------------
116             # IN: 1 class
117             # 2 numeric value to check
118              
119 3     3 0 16 sub VERSION { 1 } #VERSION
120              
121             #-------------------------------------------------------------------------------
122             # IN: 1 class to bless with
123             # 2 mode string (ignored)
124             # 3 file handle of PerlIO layer below (ignored)
125             # OUT: 1 blessed object
126              
127 9     9 0 7754 sub PUSHED { bless \*PUSHED, $_[0] } #PUSHED
128              
129             #-------------------------------------------------------------------------------
130             # IN: 1 instantiated object (ignored)
131             # 2 handle to read from
132             # OUT: 1 decoded string
133              
134             sub FILL {
135              
136             # huh?
137 1     1 0 2 local( $_ )= ref( $_[0] );
138 1 0       22 die "Class $_ was not activated" if !m#::rot(?:0|26)$#;
139              
140 1         7 return readline( $_[1] );
141             } #FILL
142              
143             #-------------------------------------------------------------------------------
144             # IN: 1 instantiated object (ignored)
145             # 2 buffer to be written
146             # 3 handle to write to
147             # OUT: 1 number of bytes written
148              
149             sub WRITE {
150              
151             # huh?
152 1     1   2 local( $_ ) = ref( $_[0] );
153 1 0       4 die "Class $_ was not activated" unless m#::rot(?:0|26)$#;
154              
155 1 0       54 return ( print { $_[2] } $_[1] ) ? length( $_[1] ) : -1;
  1         3  
156             } #WRITE
157              
158             #-------------------------------------------------------------------------------
159              
160             __END__