File: | blib/lib/Acme/OneBit.pm |
Coverage: | 45.7% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
1 | package Acme::Onebit; | |||||
2 | ||||||
3 | $VERSION = 0.01; | |||||
4 | ||||||
5 | sub encode { | |||||
6 | 0 | 0 | my @bits = split //, shift; | |||
7 | 0 | 0 | my $c = 0; | |||
8 | 0 | 0 | my $buffer; | |||
9 | ||||||
10 | 0 | 0 | for $bit(@bits){ | |||
11 | 0 | 0 | $bit = ord $bit; | |||
12 | 0 | 0 | my $ch; | |||
13 | 0 | 0 | if ($c > $bit){ | |||
14 | 0 | 0 | $ch = '.' x ($bit+255-$c); | |||
15 | }elsif($c < $bit){ | |||||
16 | 0 | 0 | $ch = '.' x ($bit-$c); | |||
17 | } | |||||
18 | 0 | 0 | $ch .= ','; | |||
19 | 0 | 0 | $buffer .= $ch; | |||
20 | 0 | 0 | $c = $bit; | |||
21 | } | |||||
22 | 0 | 0 | $buffer = join "\n", ($buffer =~ /(.{1,60})/g); | |||
23 | 0 | 0 | $buffer; | |||
24 | } | |||||
25 | sub decode { | |||||
26 | 1 | 5 | my $c = 0; | |||
27 | 1 | 4 | my $buffer; | |||
28 | 1 | 859 | for my $b(split //, shift){ | |||
29 | 1848 | 13560 | $c++ if $b eq '.'; | |||
30 | 1848 | 12137 | $c = 0 if $c == 255; | |||
31 | 1848 | 15170 | $buffer .= chr $c if $b eq ','; | |||
32 | } | |||||
33 | 1 | 3 | $buffer; | |||
34 | } | |||||
35 | sub signed { | |||||
36 | 1 | 24 | $_[0] =~ /^[.,\n]+$/ | |||
37 | } | |||||
38 | ||||||
39 | open 0 or print "Can't execute '$0'\n" and exit; | |||||
40 | ||||||
41 | (my $program = join "", <0>) =~ s/.*^\s*use\s+Acme::OneBit\s*;\n//sm; | |||||
42 | ||||||
43 | local $SIG{__WARN__} = \&garbled; | |||||
44 | ||||||
45 | do { | |||||
46 | eval decode $program; | |||||
47 | exit | |||||
48 | } if signed $program; | |||||
49 | ||||||
50 | open 0, ">$0" or print "Can't bit-ise '$0'\n" and exit; | |||||
51 | print {0} "use Acme::OneBit;\n", encode $program and exit; | |||||
52 |