File Coverage

blib/lib/Text/Annotated/Writer.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Text::Annotated::Writer;
2             # $Id: Writer.pm,v 1.7 2007-05-12 18:39:16 wim Exp $
3 1     1   801 use strict;
  1         2  
  1         37  
4 1     1   6 use vars qw($VERSION);
  1         1  
  1         44  
5 1     1   670 use Text::Annotated::Line;
  1         3  
  1         33  
6 1     1   430 use Text::Filter;
  0            
  0            
7             use base qw(Text::Filter);
8             $VERSION = '0.04';
9              
10             sub new {
11             my $proto = shift;
12             my $pkg = ref($proto) || $proto;
13             my %arg = @_;
14              
15             # initialize the no_annotation flag
16             my $no_annotation = $arg{no_annotation} || 0; # defaults to printing of the annotations
17             delete $arg{no_annotation};
18              
19             # construct the filter
20             my $this = $pkg->SUPER::new(input => [], %arg);
21             bless $this, $pkg;
22              
23             # add the fields
24             $this->{no_annotation} = $no_annotation;
25              
26             # return the filter
27             return $this;
28             }
29              
30             sub set_input { # overrides set_input in Text::Filter, adding a type check
31             my ($this,$input,$postread) = @_;
32              
33             ref($input) eq 'ARRAY' or
34             die "Invalid input specified,\n"
35             . "Text::Annotated::Writer expects an array of Text::Annotated::Line objects,\n"
36             . "stopped";
37              
38             $this->SUPER::set_input($input,$postread);
39             }
40              
41             sub write {
42             my Text::Annotated::Writer $this = shift;
43             my $na = $this->{no_annotation};
44             while(defined(my $line = $this->readline)) {
45             $this->writeline($na
46             ? "$line" # this will work both on annotated lines and ordinary strings
47             : $line->stringify_annotated
48             );
49             }
50             }
51              
52             sub run { # needed when using the filter in a Text::Filter::Chain
53             my $this = shift;
54             $this->write(@_);
55             }
56              
57             sub writer { # constructs and runs the filter
58             my $proto = shift;
59             my $this = $proto->new(@_);
60             $this->write;
61             return $this;
62             }
63              
64             1;
65              
66             __END__