lib/Zen/Koan.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 42 | 46 | 91.3 |
branch | 11 | 12 | 91.6 |
condition | 6 | 7 | 85.7 |
subroutine | 9 | 9 | 100.0 |
pod | 4 | 5 | 80.0 |
total | 72 | 79 | 91.1 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package Zen::Koan; | ||||||
2 | 3 | 3 | 50446 | use strict; | |||
3 | 5 | ||||||
3 | 184 | ||||||
3 | 3 | 3 | 17 | use warnings; | |||
3 | 6 | ||||||
3 | 83 | ||||||
4 | 3 | 3 | 16 | use Carp qw/croak/; | |||
3 | 5 | ||||||
3 | 1779 | ||||||
5 | |||||||
6 | our $VERSION = '0.02'; | ||||||
7 | |||||||
8 | sub new { | ||||||
9 | 109 | 109 | 1 | 3332 | my $class = shift; | ||
10 | 109 | 114 | my %opts; | ||||
11 | 109 | 100 | 66 | 492 | if (@_ == 1 and ref($_[0]) eq 'HASH') { | ||
12 | 103 | 140 | %opts = %{ $_[0] }; | ||||
103 | 470 | ||||||
13 | } | ||||||
14 | 6 | 17 | else { %opts = @_ } | ||||
15 | 109 | 100 | 274 | $opts{title} ||= 'A koan by no other name'; | |||
16 | 109 | 100 | 193 | $opts{body} ||= 'This koan offers little wisdom. It just is.'; | |||
17 | |||||||
18 | 109 | 380 | my $self = { title => $opts{title}, | ||||
19 | body => $opts{body}, | ||||||
20 | indent_level => 0, | ||||||
21 | current_indent => 0, | ||||||
22 | }; | ||||||
23 | |||||||
24 | 109 | 191 | bless $self, $class; | ||||
25 | 109 | 307 | return $self; | ||||
26 | } | ||||||
27 | |||||||
28 | 5 | 5 | 1 | 2670 | sub title { $_[0]->{title} } | ||
29 | 4 | 4 | 1 | 25 | sub body { $_[0]->{body} } | ||
30 | |||||||
31 | sub as_html { | ||||||
32 | 4 | 4 | 1 | 10 | my $self = shift; | ||
33 | 4 | 8 | my $body = ''; | ||||
34 | 4 | 23 | for my $p (split "\n", $self->{body}) { | ||||
35 | 13 | 100 | 54 | next if $p =~ /^\s*$/; | |||
36 | 12 | 20 | chomp $p; | ||||
37 | 12 | 100 | 77 | if ($p =~ s/^(\s+)//) { | |||
100 | |||||||
38 | 2 | 4 | my $indent = length $1; | ||||
39 | 2 | 100 | 10 | if ($indent > $self->{current_indent}) { | |||
50 | |||||||
40 | 1 | 2 | $self->{indent_level}++; | ||||
41 | 1 | 4 | $body .= "\n"; |
||||
42 | } | ||||||
43 | elsif ($indent < $self->{current_indent}) { | ||||||
44 | 0 | 0 | $self->{indent_level}--; | ||||
45 | 0 | 0 | $body .= "\n"; | ||||
46 | } | ||||||
47 | 2 | 5 | $self->{current_indent} = $indent; | ||||
48 | } | ||||||
49 | elsif ($self->{indent_level}) { | ||||||
50 | 1 | 4 | while ($self->{indent_level}) { | ||||
51 | 1 | 2 | $self->{indent_level}--; | ||||
52 | 1 | 4 | $body .= "\n"; | ||||
53 | } | ||||||
54 | } | ||||||
55 | 12 | 40 | $body .= " $p \n"; |
||||
56 | } | ||||||
57 | 4 | 20 | while ($self->{indent_level}) { | ||||
58 | 0 | 0 | $self->{indent_level}--; | ||||
59 | 0 | 0 | $body .= "\n"; | ||||
60 | } | ||||||
61 | 4 | 33 | return < | ||||
62 | $self->{title} |
||||||
63 | |
||||||
64 | $body | ||||||
65 | EOT | ||||||
66 | } | ||||||
67 | |||||||
68 | sub as_text { | ||||||
69 | 102 | 102 | 0 | 112 | my $self = shift; | ||
70 | 102 | 935 | return "\t$self->{title}\n\n$self->{body}"; | ||||
71 | } | ||||||
72 | |||||||
73 | sub AUTOLOAD { | ||||||
74 | return < | ||||||
75 | You are expecting too much from this koan. | ||||||
76 | |||||||
77 | Look within for more answers. | ||||||
78 | EOT | ||||||
79 | 1 | 1 | 482 | } | |||
80 | |||||||
81 | 1; | ||||||
82 | |||||||
83 | __END__ |