File Coverage

inc/Pegex/Input.pm
Criterion Covered Total %
statement 23 32 71.8
branch 9 20 45.0
condition 1 3 33.3
subroutine 4 4 100.0
pod 0 3 0.0
total 37 62 59.6


line stmt bran cond sub pod time code
1             package Pegex::Input;
2              
3 1     1   5 use Pegex::Base;
  1         2  
  1         6  
4              
5             has string => ();
6             has stringref => ();
7             has file => ();
8             has handle => ();
9             has _buffer => ();
10             has _is_eof => 0;
11             has _is_open => 0;
12             has _is_close => 0;
13              
14             # NOTE: Current implementation reads entire input into _buffer on open().
15             sub read {
16 275     275 0 579 my ($self) = @_;
17 275 50       873 die "Attempted Pegex::Input::read before open" if not $self->{_is_open};
18 275 50       952 die "Attempted Pegex::Input::read after EOF" if $self->{_is_eof};
19              
20 275         528 my $buffer = $self->{_buffer};
21 275         517 $self->{_buffer} = undef;
22 275         497 $self->{_is_eof} = 1;
23              
24 275         1382 return $buffer;
25             }
26              
27             sub open {
28 275     275 0 895 my ($self) = @_;
29 275 50 33     1807 die "Attempted to reopen Pegex::Input object"
30             if $self->{_is_open} or $self->{_is_close};
31              
32 275 50       2200 if (my $ref = $self->{stringref}) {
    50          
    50          
    50          
33 0         0 $self->{_buffer} = $ref;
34             }
35             elsif (my $handle = $self->{handle}) {
36 0         0 $self->{_buffer} = \ do { local $/; <$handle> };
  0         0  
  0         0  
37             }
38             elsif (my $path = $self->{file}) {
39 0 0       0 open my $handle, $path
40             or die "Pegex::Input can't open $path for input:\n$!";
41 0         0 $self->{_buffer} = \ do { local $/; <$handle> };
  0         0  
  0         0  
42             }
43             elsif (exists $self->{string}) {
44 275         1049 $self->{_buffer} = \$self->{string};
45             }
46             else {
47 0         0 die "Pegex::Input::open failed. No source to open";
48             }
49 275         605 $self->{_is_open} = 1;
50 275         711 return $self;
51             }
52              
53             sub close {
54 275     275 0 499 my ($self) = @_;
55 275 50       984 die "Attempted to close an unopen Pegex::Input object"
56             if $self->{_is_close};
57 275 50       1055 close $self->{handle} if $self->{handle};
58 275         497 $self->{_is_open} = 0;
59 275         504 $self->{_is_close} = 1;
60 275         529 $self->{_buffer} = undef;
61 275         737 return $self;
62             }
63              
64             1;