| 1 | package Text::Annotated::Reader; | ||||||||||||||||||||
| 2 | # $Id: Reader.pm,v 1.7 2007-05-12 18:39:16 wim Exp $ | ||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use Text::Filter; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use Text::Annotated::Line; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use base qw(Text::Filter); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | $VERSION = '0.04'; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||
| 11 | 1 | my $proto = shift; | |||||||||||||||||||
| 12 | 1 | ▶ | my $pkg = ref($proto) || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 13 | 1 | my $this = $pkg->SUPER::new(output => [], @_); | |||||||||||||||||||
| 14 | 1 | bless $this, $pkg; | |||||||||||||||||||
| 15 | } | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 1 | ▶ | sub set_input { # overrides set_input of Text::Filter, adds a type check | ||||||||||||||||||
|
Subroutine
set_input: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 18 | 1 | my ($this,$input,$postread) = @_; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # the input needs to be specified as a filename | ||||||||||||||||||||
| 21 | 1 | ▶ | !ref($input) or | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | die "Invalid input specified.\n" | ||||||||||||||||||||
| 23 | . "Text::Annotated::Reader expects a filename,\n" | ||||||||||||||||||||
| 24 | . "stopped"; | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 1 | $this->SUPER::set_input($input,$postread); | |||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 1 | ▶ | sub set_output { # overrides set_output of Text::Filter, adds some magic | ||||||||||||||||||
|
Subroutine
set_output: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 30 | 1 | my ($this,$output,$prewrite) = @_; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | # the output needs to be a ref to an array | ||||||||||||||||||||
| 33 | 1 | ▶ | ref($output) eq 'ARRAY' or | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 1 | my $linenr = 1; # index of first line | |||||||||||||||||||
| 39 | 1 | ▶ | my $magic_handler = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 40 | my $line = new Text::Annotated::Line( | ||||||||||||||||||||
| 41 | filename => $this->{input}, | ||||||||||||||||||||
| 42 | 1 | linenr => $linenr++, | |||||||||||||||||||
| 43 | content => $_[0], | ||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||
| 45 | 0 | push @$output, $line; | |||||||||||||||||||
| 46 | 1 | }; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | # keep the array in a separate field | ||||||||||||||||||||
| 49 | 1 | $this->{annotated_lines} = $output; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 1 | $this->SUPER::set_output($magic_handler,$prewrite); | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 1 | ▶ | sub read { # reads and annotates an entire file | ||||||||||||||||||
|
Subroutine
read: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||
| 55 | 1 | my Text::Annotated::Reader $this = shift; | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | # autogenerate an output array if no output is specified | ||||||||||||||||||||
| 58 | 1 | ▶ | $this->set_output([]) unless defined $this->{annotated_lines}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | # copy the lines from input to output, letting the output handler | ||||||||||||||||||||
| 61 | # do the annotation | ||||||||||||||||||||
| 62 | 1 | ▶ | while(defined(my $line = $this->readline())) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | 1 | $this->writeline($line); | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | ▶ | sub run { # needed when using the filter in a Text::Filter::Chain | ||||||||||||||||||
|
Subroutine
run: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 68 | 0 | my $this = shift; | |||||||||||||||||||
| 69 | 0 | $this->read(@_); | |||||||||||||||||||
| 70 | 0 | ▶ | return wantarray ? @{$this->{annotated_lines}} : $this->{annotated_lines}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 1 | ▶ | sub reader { # constructs and runs a filter | ||||||||||||||||||
|
Subroutine
reader: called CC=1 SCAR=0.2
Pod
undocumented
| |||||||||||||||||||||
| 74 | 1 | my $proto = shift; | |||||||||||||||||||
| 75 | 1 | my $this = $proto->new(@_); | |||||||||||||||||||
| 76 | 1 | $this->read; | |||||||||||||||||||
| 77 | 0 | return $this; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 1 | 1; | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | __END__ | ||||||||||||||||||||