File Coverage

blib/lib/Text/Annotated/Reader.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Text::Annotated::Reader;
2             # $Id: Reader.pm,v 1.7 2007-05-12 18:39:16 wim Exp $
3 1     1   797 use strict;
  1         4  
  1         49  
4 1     1   6 use vars qw($VERSION);
  1         1  
  1         51  
5 1     1   446 use Text::Filter;
  0            
  0            
6             use Text::Annotated::Line;
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 $this = $pkg->SUPER::new(output => [], @_);
14             bless $this, $pkg;
15             }
16              
17             sub set_input { # overrides set_input of Text::Filter, adds a type check
18             my ($this,$input,$postread) = @_;
19            
20             # the input needs to be specified as a filename
21             !ref($input) or
22             die "Invalid input specified.\n"
23             . "Text::Annotated::Reader expects a filename,\n"
24             . "stopped";
25            
26             $this->SUPER::set_input($input,$postread);
27             }
28              
29             sub set_output { # overrides set_output of Text::Filter, adds some magic
30             my ($this,$output,$prewrite) = @_;
31              
32             # the output needs to be a ref to an array
33             ref($output) eq 'ARRAY' or
34             die "Invalid output. Text::Annotated::Writer expects a ref to an array,\n"
35             . "stopped";
36              
37             # construct a magic handler which actually annotates the lines
38             my $linenr = 1; # index of first line
39             my $magic_handler = sub {
40             my $line = new Text::Annotated::Line(
41             filename => $this->{input},
42             linenr => $linenr++,
43             content => $_[0],
44             );
45             push @$output, $line;
46             };
47              
48             # keep the array in a separate field
49             $this->{annotated_lines} = $output;
50              
51             $this->SUPER::set_output($magic_handler,$prewrite);
52             }
53              
54             sub read { # reads and annotates an entire file
55             my Text::Annotated::Reader $this = shift;
56              
57             # autogenerate an output array if no output is specified
58             $this->set_output([]) unless defined $this->{annotated_lines};
59              
60             # copy the lines from input to output, letting the output handler
61             # do the annotation
62             while(defined(my $line = $this->readline())) {
63             $this->writeline($line);
64             }
65             }
66              
67             sub run { # needed when using the filter in a Text::Filter::Chain
68             my $this = shift;
69             $this->read(@_);
70             return wantarray ? @{$this->{annotated_lines}} : $this->{annotated_lines};
71             }
72              
73             sub reader { # constructs and runs a filter
74             my $proto = shift;
75             my $this = $proto->new(@_);
76             $this->read;
77             return $this;
78             }
79              
80             1;
81              
82             __END__