File Coverage

blib/lib/YAML/PP/Reader.pm
Criterion Covered Total %
statement 39 48 81.2
branch 8 12 66.6
condition 3 8 37.5
subroutine 11 13 84.6
pod 0 5 0.0
total 61 86 70.9


line stmt bran cond sub pod time code
1             # ABSTRACT: Reader class for YAML::PP representing input data
2 51     51   144487 use strict;
  51         84  
  51         1571  
3 51     51   954 use warnings;
  51         76  
  51         18596  
4             package YAML::PP::Reader;
5              
6             our $VERSION = 'v0.39.0'; # VERSION
7              
8 3     3 0 7 sub input { return $_[0]->{input} }
9 0     0 0 0 sub set_input { $_[0]->{input} = $_[1] }
10              
11             sub new {
12 12277     12277 0 23736 my ($class, %args) = @_;
13 12277         17524 my $input = delete $args{input};
14 12277         47344 return bless {
15             input => $input,
16             }, $class;
17             }
18              
19             sub read {
20 1     1 0 2 my ($self) = @_;
21 1   50     3 my $pos = pos $self->{input} || 0;
22 1         8 my $yaml = substr($self->{input}, $pos);
23 1         2 $self->{input} = '';
24 1         3 return $yaml;
25             }
26              
27             sub readline {
28 40133     40133 0 49050 my ($self) = @_;
29 40133 100       69424 unless (length $self->{input}) {
30 4448         7505 return;
31             }
32 35685 50       148197 if ( $self->{input} =~ m/\G([^\r\n]*(?:\n|\r\n|\r|\z))/g ) {
33 35685         66759 my $line = $1;
34 35685 100       53113 unless (length $line) {
35 7379         11909 $self->{input} = '';
36 7379         14178 return;
37             }
38 28306         56793 return $line;
39             }
40 0         0 return;
41             }
42              
43             package YAML::PP::Reader::File;
44              
45 51     51   317 use Scalar::Util qw/ openhandle /;
  51         82  
  51         3652  
46              
47             our @ISA = qw/ YAML::PP::Reader /;
48              
49 51     51   265 use Carp qw/ croak /;
  51         84  
  51         13285  
50              
51             sub open_handle {
52 19 100   19   84 if (openhandle( $_[0]->{input} )) {
53 1         4 return $_[0]->{input};
54             }
55             open my $fh, '<:encoding(UTF-8)', $_[0]->{input}
56 18 50   4   1083 or croak "Could not open '$_[0]->{input}' for reading: $!";
  4         2581  
  4         55  
  4         18  
57 18         4827 return $fh;
58             }
59              
60             sub read {
61 0   0 0   0 my $fh = $_[0]->{filehandle} ||= $_[0]->open_handle;
62 0 0       0 if (wantarray) {
63 0         0 my @yaml = <$fh>;
64 0         0 return @yaml;
65             }
66             else {
67 0         0 local $/;
68 0         0 my $yaml = <$fh>;
69 0         0 return $yaml;
70             }
71             }
72              
73             sub readline {
74 974   66 974   1719 my $fh = $_[0]->{filehandle} ||= $_[0]->open_handle;
75 974         4877 return scalar <$fh>;
76             }
77              
78             1;