blib/lib/IO/YAML.pm

filter: Statementstmt 75.8% 192 / 253 Branchbran 59.0% 65 / 110 Conditioncond 32.8% 21 / 64 MC/DCmcdc 6.2% 2 / 32 Subroutinesub 74.5% 44 / 59 Podpod 51.8% 14 / 27 totaltotal 62.0% 338 / 545 SCAR 59.3
CC
83
Cov
65%
truncate
30.0
open
21.7
tell
20.0
CRAP 375.7
↑ Summary Build »
1 package IO::YAML;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use YAML qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Fcntl qw(:seek);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use vars qw($VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 $VERSION = '0.08';
15  
16 sub new {
17 12 ▶ my ($cls, @args) = @_;
Subroutine new: called CC=11 SCAR=26.5
Pod documented
18 12 my %args;
19 12 ▶ if (UNIVERSAL::isa($args[0], 'GLOB')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($args[0], 'GLOB')) { } 4 8 12
elsif (scalar @args >= 2 and $args[1] =~ /^\+?[<>rwa]|>>|\d+$/ and $args[0] ne 'mode') { } 0 16 16
20 # IO::YAML->new($fh)
21 # IO::YAML->new($fh, $mode, %opt_args)
22 # IO::YAML->new($fh, %args)
23 4 $args{'handle'} = shift @args;
24 4 ▶ $args{'mode'} = shift @args
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@args) % 2 and $args[0] =~ /^\+?[<>rwa]|>>|\d+$/ 3 1 4
Condition66%2/3
scalar(@args) % 2 and $args[0] =~ /^\+?[<>rwa]|>>|\d+$/
!ll&&!rl&&r
103
MC/DC50%1/2
scalar(@args) % 2 and $args[0] =~ /^\+?[<>rwa]|>>|\d+$/
scalar(@args) % 2 $args[0] =~ /^\+?[<>rwa]|>>|\d+$/
Truth table66%2/3
A and B
A= scalar(@args) % 2B= $args[0] =~ /^\+?[<>rwa]|>>|\d+$/
ABresult
0X0
100
111
25 if scalar(@args) % 2
26 && $args[0] =~ /^\+?[<>rwa]|>>|\d+$/;
27 }
28 ▶ elsif (scalar(@args) >= 2
Condition33%2/6
scalar @args >= 2 and $args[1] =~ /^\+?[<>rwa]|>>|\d+$/ and $args[0] ne "mode"
!ll&&!rl&&r
800
scalar @args >= 2 and $args[1] =~ /^\+?[<>rwa]|>>|\d+$/
!ll&&!rl&&r
800
MC/DC0%0/3
scalar @args >= 2 and $args[1] =~ /^\+?[<>rwa]|>>|\d+$/ and $args[0] ne "mode"
scalar @args >= 2 $args[1] =~ /^\+?[<>rwa]|>>|\d+$/ $args[0] ne "mode"
Truth table25%1/4
A and B and C
A= scalar @args >= 2B= $args[1] =~ /^\+?[<>rwa]|>>|\d+$/C= $args[0] ne "mode"
ABCresult
0XX0
10X0
1100
1111
29 && $args[1] =~ /^\+?[<>rwa]|>>|\d+$/
30 && $args[0] ne 'mode') {
31 # IO::YAML->new($path, $mode)
32 # IO::YAML->new($path, $mode, %args)
33 0 $args{'mode'} = splice(@args, 1, 1);
34 }
35 12 ▶ if (scalar(@args) % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@args) % 2) 3 9 12
36 # --- Odd number of args
37 3 ▶ if (ref($args[0]) eq ''
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq '' or UNIVERSAL::can($args[0], 'stringify')) { } 3 0 3
Condition33%1/3
ref $args[0] eq "" or UNIVERSAL::can($args[0], "stringify")
l!l&&r!l&&!r
300
MC/DC0%0/2
ref $args[0] eq "" or UNIVERSAL::can($args[0], "stringify")
ref $args[0] eq "" UNIVERSAL::can($args[0], "stringify")
Truth table33%1/3
A or B
A= ref $args[0] eq ""B= UNIVERSAL::can($args[0], "stringify")
ABresult
1X1
011
000
38 or UNIVERSAL::can($args[0], 'stringify')) {
39 3 unshift @args, 'path';
40 }
41 else {
42 0 die "Odd argument can't be interpreted";
43 }
44 }
45 %args = (
46 12 %args,
47 'auto_load' => 0,
48 'auto_terminate' => 0,
49 @args,
50 'buffer' => [],
51 );
52 12 my $self = bless Symbol::gensym(), $cls;
53 12 foreach (keys %args) {
54 46 *$self->{$_} = $args{$_};
55 }
56 12 return $self->init;
57 }
58  
59 21 ▶sub path { scalar @_ > 1 ? *{$_[0]}->{'path'} = $_[1] : *{$_[0]}->{'path'} }
Subroutine path: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 1 ? : 9 12 21
60 33 ▶sub mode { scalar @_ > 1 ? *{$_[0]}->{'mode'} = $_[1] : *{$_[0]}->{'mode'} }
Subroutine mode: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 1 ? : 22 11 33
61 32 ▶sub auto_load { scalar @_ > 1 ? *{$_[0]}->{'auto_load'} = $_[1] : *{$_[0]}->{'auto_load'} }
Subroutine auto_load: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 1 ? : 5 27 32
62 9 ▶sub auto_terminate { scalar @_ > 1 ? *{$_[0]}->{'auto_terminate'} = $_[1] : *{$_[0]}->{'auto_terminate'} }
Subroutine auto_terminate: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 1 ? : 1 8 9
63  
64 21 ▶sub auto_close { scalar @_ > 1 ? *{$_[0]}->{'auto_close'} = $_[1] : *{$_[0]}->{'auto_close'} }
Subroutine auto_close: called CC=2 SCAR=8.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 1 ? : 21 0 21
65 30 ▶sub buffer { scalar @_ > 1 ? *{$_[0]}->{'buffer'} = $_[1] : *{$_[0]}->{'buffer'} }
Subroutine buffer: called CC=2 SCAR=8.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 1 ? : 0 30 30
66 125 ▶sub handle { scalar @_ > 1 ? *{$_[0]}->{'handle'} = $_[1] : *{$_[0]}->{'handle'} }
Subroutine handle: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 1 ? : 22 103 125
67  
68 sub terminate {
69 1 ▶ my ($self) = @_;
Subroutine terminate: called CC=4 SCAR=14.3
Pod undocumented
70 1 my $fh = $self->handle;
71 1 ▶ die "Can't terminate a document in an unopened stream"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 1 1
72 unless defined $fh;
73 1 my $mode = $self->mode;
74 1 ▶ die "Can't terminate a document in a stream opened for read-only access"
Branch: 1 branch
BranchTrueFalseTotal
if $mode =~ /^[r<]$/ 0 1 1
75 if $mode =~ /^[r<]$/;
76 1 ▶ print $fh "...\n" or die "Couldn't terminate document: $!";
Branch: 1 branch
BranchTrueFalseTotal
print $fh "...\n" or die "Couldn't terminate document: $!" 0 1 1
77 1 return $fh;
78 }
79  
80 sub open {
81 9 ▶ my ($self, $path, $mode) = @_;
Subroutine open: called CC=10 SCAR=30.8
Pod documented
82
83 9 my $fh = $self->handle;
84 9 ▶ if (defined $path and defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path and defined $fh) 0 9 9
Condition33%1/3
defined $path and defined $fh
!ll&&!rl&&r
090
MC/DC0%0/2
defined $path and defined $fh
defined $path defined $fh
Truth table33%1/3
A and B
A= defined $pathB= defined $fh
ABresult
0X0
100
111
85 # --- Reopen a different file
86 0 $self->close;
87 0 undef $fh;
88 }
89
90 9 ▶ if (defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) { } 0 9 9
91 # Default is to read it
92 0 ▶ $mode = '<' unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 0 0 0
93 }
94 else {
95
96 9 ▶ $path ||= $self->path;
Condition33%1/3
$path ||= $self->path
l!l&&r!l&&!r
900
MC/DC0%0/2
$path ||= $self->path
$path $self->path
Truth table33%1/3
A ||= B
A= $pathB= $self->path
ABresult
1X1
011
000
97
98 9 ▶ unless (defined $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 0 9 9
99 # $! = "No such file or directory";
100 0 ▶ if (exists &Errno::ENOENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists &Errno::ENOENT) { } 0 0 0
101 0 $! = &Errno::ENOENT;
102 }
103 else {
104 0 CORE::open(gensym, undef);
105 }
106 0 return;
107 }
108
109 9 $fh = IO::File->new;
110 9 $self->handle($fh);
111
112 9 ▶ ($path, $mode) = $self->normalize_path_and_mode($path, $mode || $self->mode);
Condition66%2/3
$mode || $self->mode
l!l&&r!l&&!r
504
MC/DC50%1/2
$mode || $self->mode
$mode $self->mode
Truth table66%2/3
A || B
A= $modeB= $self->mode
ABresult
1X1
011
000
113 9 $self->path($path);
114 9 $self->mode($mode);
115
116 9 ▶ unless ($fh->open($path, $mode)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open($path, $mode)) 0 9 9
117 0 $self->handle(undef);
118 #unlink $path
119 # if -e $path and $mode eq '>';
120 0 return;
121 }
122
123 9 $self->auto_close(1);
124
125 }
126
127 9 $! = 0;
128 9 return $fh;
129
130 }
131  
132 sub close {
133 13 ▶ my ($self) = @_;
Subroutine close: called CC=3 SCAR=11.0
Pod documented
134 # return unless $self->auto_close;
135 13 my $fh = $self->handle;
136 13 ▶ if (defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) 13 0 13
137 13 fh_close($fh);
138 13 $self->handle(undef);
139 }
140 13 $self->mode(undef);
141 13 undef *$self->{$_} for qw(mode);
142 13 return $self;
143 }
144  
145 sub print {
146 7 ▶ my $self = shift;
Subroutine print: called CC=6 SCAR=19.2
Pod documented
147 7 ▶ my $fh = $self->handle || $self->open || die "Can't open: $!";
Condition40%2/5
$self->handle || $self->open || die("Can't open: $!")
l!l
70
$self->handle or $self->open
l!l&&r!l&&!r
700
MC/DC0%0/2
$self->handle || $self->open || die("Can't open: $!")
$self->handle $self->open
Truth table33%1/3
A or B
A= $self->handleB= $self->open
ABresult
1X1
011
000
148 7 ▶ my @terminator = $self->auto_terminate ? ("...\n") : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->auto_terminate ? : 2 5 7
149 print $fh (YAML::Dump($_), @terminator) or die $!
150 7 ▶ foreach @_;
Branch: 1 branch
BranchTrueFalseTotal
print $fh YAML::Dump($_), @terminator or die $! 0 19 19
151 7 return 1;
152 }
153  
154 sub getline {
155 27 ▶ my ($self) = @_;
Subroutine getline: called CC=10 SCAR=23.2
Pod documented
156 27 ▶ my $fh = $self->handle || $self->open || die "Can't open: $!";
Condition40%2/5
$self->handle || $self->open || die("Can't open: $!")
l!l
270
$self->handle or $self->open
l!l&&r!l&&!r
2700
MC/DC0%0/2
$self->handle || $self->open || die("Can't open: $!")
$self->handle $self->open
Truth table33%1/3
A or B
A= $self->handleB= $self->open
ABresult
1X1
011
000
157 27 my $buffer = $self->buffer;
158 27 my $source;
159 27 while (1) {
160 31 ▶ $source = scalar @$buffer ? shift @$buffer : <$fh>;
Branch: 1 branch
BranchTrueFalseTotal
scalar @$buffer ? : 22 9 31
161 31 ▶ return unless defined $source;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source 0 31 31
162 31 ▶ last unless $source =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
unless $source =~ /^#/ 27 4 31
163 }
164 27 ▶ while (defined(my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 71 2 73
165 71 ▶ if ($line =~ /^\.\.\.$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\.\.\.$/) { } 3 68 71
elsif ($line =~ /^---/) { } 22 46 68
166 # End of stream
167 3 last;
168 }
169 elsif ($line =~ /^---/) {
170 # Oops -- hit start of next document in stream
171 22 push @$buffer, $line;
172 22 last;
173 }
174 46 $source .= $line;
175 }
176 27 ▶ my $retval = $self->auto_load ? YAML::Load($source) : $source;
Branch: 1 branch
BranchTrueFalseTotal
$self->auto_load ? : 16 11 27
177 27 return $retval;
178 }
179  
180 sub getlines {
181 2 ▶ my ($self) = @_;
Subroutine getlines: called CC=3 SCAR=12.0
Pod documented
182 2 ▶ my $fh = $self->handle || $self->open || die "Can't open: $!";
Condition40%2/5
$self->handle || $self->open || die("Can't open: $!")
l!l
20
$self->handle or $self->open
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->handle || $self->open || die("Can't open: $!")
$self->handle $self->open
Truth table33%1/3
A or B
A= $self->handleB= $self->open
ABresult
1X1
011
000
183 2 my @lines = <$fh>;
184 2 return YAML::Load(join('', @lines));
185 }
186  
187 sub next {
188 0 ▶ goto &getlines if wantarray;
Subroutine next: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
189 0 goto &getline;
190 }
191  
192 sub seek {
193 3 ▶ my ($self, $pos, $whence) = @_;
Subroutine seek: called CC=8 SCAR=29.8
Pod documented
194 3 ▶ my $fh = $self->handle || $self->open || die "Can't open: $!";
Condition40%2/5
$self->handle || $self->open || die("Can't open: $!")
l!l
30
$self->handle or $self->open
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->handle || $self->open || die("Can't open: $!")
$self->handle $self->open
Truth table33%1/3
A or B
A= $self->handleB= $self->open
ABresult
1X1
011
000
195 3 ▶ my $result = fh_seek($fh, $pos, $whence)
Condition50%1/2
fh_seek($fh, $pos, $whence) || die("Couldn't seek: $!")
l!l
30
MC/DC0%0/1
fh_seek($fh, $pos, $whence) || die("Couldn't seek: $!")
fh_seek($fh, $pos, $whence)
Truth table50%1/2
A
A= fh_seek($fh, $pos, $whence)
Aresult
11
00
196 || die "Couldn't seek: $!";
197 3 my $old_pos = fh_tell($fh);
198 3 my $buffer = $self->buffer;
199 3 my $source;
200 3 ▶ if ($pos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos) { } 0 3 3
201 # Arbitrary seek -- make sure we're at the beginning of a YAML document
202 0 ▶ $result = fh_seek($fh, $pos, $whence)
Branch: 1 branch
BranchTrueFalseTotal
$result = fh_seek($fh, $pos, $whence) or die "Couldn't seek: $!" 0 0 0
203 or die "Couldn't seek: $!";
204 0 $source = <$fh>;
205 0 ▶ if (!defined($source)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $source) { } 0 0 0
elsif (not $source =~ /^---(?=\s)/) { } 0 0 0
206 # We're at the end of the stream -- that's fine, just
207 # set the buffer to the empty string
208 0 @$buffer = ();
209 }
210 elsif ($source !~ /^---(?=\s)/) {
211 # Oops! We were expecting the '---' (etc.) line that
212 # begins a YAML document, but we found something else.
213 # Try to put things back the way they were, then die.
214 0 fh_seek($fh, $old_pos, SEEK_SET);
215 0 die "Seek not allowed except to start of YAML document";
216 }
217 }
218 else {
219 # Clear the buffer
220 3 @$buffer = ();
221 }
222 3 return $result;
223 }
224  
225 sub tell {
226 0 ▶ my ($self) = @_;
Subroutine tell: not called CC=4 SCAR=30.0
Pod documented
227 0 ▶ my $fh = $self->handle || $self->open || die "Can't open: $!";
Condition0%0/5
$self->handle || $self->open || die("Can't open: $!")
l!l
00
$self->handle or $self->open
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->handle || $self->open || die("Can't open: $!")
$self->handle $self->open
Truth table0%0/3
A or B
A= $self->handleB= $self->open
ABresult
1X1
011
000
228 0 my $pos = fh_tell($fh);
229 0 ▶ die "Can't get file cursor position: $!"
Branch: 1 branch
BranchTrueFalseTotal
unless $! eq "" 0 0 0
230 unless $! eq '';
231 0 return $pos;
232 }
233  
234 sub truncate {
235 0 ▶ my ($self, $length) = @_;
Subroutine truncate: not called CC=5 SCAR=34.0
Pod documented
236 0 ▶ die "Arbitrary truncates not allowed"
Branch: 1 branch
BranchTrueFalseTotal
unless $length == 0 or $length == $self->tell 0 0 0
Condition0%0/3
$length == 0 or $length == $self->tell
l!l&&r!l&&!r
000
MC/DC0%0/2
$length == 0 or $length == $self->tell
$length == 0 $length == $self->tell
Truth table0%0/3
A or B
A= $length == 0B= $length == $self->tell
ABresult
1X1
011
000
237 unless $length == 0
238 or $length == $self->tell;
239 0 ▶ my $fh = $self->handle || $self->open || die "Can't open: $!";
Condition0%0/5
$self->handle || $self->open || die("Can't open: $!")
l!l
00
$self->handle or $self->open
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->handle || $self->open || die("Can't open: $!")
$self->handle $self->open
Truth table0%0/3
A or B
A= $self->handleB= $self->open
ABresult
1X1
011
000
240 0 fh_truncate($fh, $length);
241 0 return $! ne '';
242 }
243  
244 sub eof {
245 3 ▶ my ($self) = @_;
Subroutine eof: called CC=3 SCAR=12.5
Pod documented
246 3 ▶ my $fh = $self->handle || $self->open || die "Can't open: $!";
Condition40%2/5
$self->handle || $self->open || die("Can't open: $!")
l!l
30
$self->handle or $self->open
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->handle || $self->open || die("Can't open: $!")
$self->handle $self->open
Truth table33%1/3
A or B
A= $self->handleB= $self->open
ABresult
1X1
011
000
247 3 return fh_eof($fh);
248 }
249  
250 sub UNTIE {
251 12 ▶ my ($self, $count) = @_;
Subroutine UNTIE: called CC=2 SCAR=7.2
252 12 ▶ $self->close if $self->handle;
Branch: 1 branch
BranchTrueFalseTotal
if $self->handle 0 12 12
253 }
254  
255 sub DESTROY {
256 12 ▶ my ($self) = @_;
Subroutine DESTROY: called CC=5 SCAR=17.5
257 12 ▶ $self->close if $self->handle;
Branch: 1 branch
BranchTrueFalseTotal
if $self->handle 7 5 12
258 12 ▶ unless ( $^V and $^V lt '5.8.0' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($^V and $^V lt "5.8.0") 12 0 12
Condition33%1/3
$^V and $^V lt "5.8.0"
!ll&&!rl&&r
0120
MC/DC0%0/2
$^V and $^V lt "5.8.0"
$^V $^V lt "5.8.0"
Truth table33%1/3
A and B
A= $^VB= $^V lt "5.8.0"
ABresult
0X0
100
111
259 12 ▶ untie *$self if tied *$self;
Branch: 1 branch
BranchTrueFalseTotal
if tied *$self 12 0 12
260 }
261 }
262  
263 sub AUTOLOAD {
264 0 ▶ my $self = shift;
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
265 0 my $fh = $self->handle;
266 0 (my $method = $AUTOLOAD) =~ s/.*:://;
267 0 my $f = UNIVERSAL::can($fh, $method);
268 0 ▶ die "Unknown method '$method' called"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f 0 0 0
269 unless defined $f;
270 0 unshift @_, $fh;
271 0 goto &$f;
272 }
273  
274 # --- Private methods
275  
276 sub normalize_path_and_mode {
277 9 ▶ my ($self, $path, $mode) = @_;
Subroutine normalize_path_and_mode: called CC=4 SCAR=14.3
Pod undocumented
278 9 ▶ if ($path =~ s/^(<|>|>>|\+<|\+>)\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ s/^(<|>|>>|\+<|\+>)\s*//) 0 9 9
279 0 $mode = $1;
280 }
281 9 ▶ return ($path, '<') unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 4 5 9
282 5 my %mode_norm = qw(
283 < <
284 r <
285 > >
286 w >
287 >> >>
288 a >>
289 +< +<
290 r+ +<
291 +> +>
292 w+ +>
293 );
294 5 ▶ $mode = $mode_norm{$mode}
Branch: 1 branch
BranchTrueFalseTotal
$mode = $mode_norm{$mode} or die "Unknown mode: '${mode}'" 0 5 5
295 or die "Unknown mode: '$mode'";
296 5 return ($path, $mode);
297 }
298  
299 sub init {
300 12 ▶ my ($self) = @_;
Subroutine init: called CC=3 SCAR=11.0
Pod undocumented
301 12 $self->auto_close(0);
302 12 my $path = $self->path;
303 12 my $fh = $self->handle;
304 12 ▶ if ($fh) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fh) { } 4 8 12
elsif (defined $path) { } 3 5 8
305 # --- Nothing to do
306 }
307 elsif (defined $path) {
308 3 $self->open($path, $self->mode);
309 }
310 else {
311 # --- Nothing to do
312 }
313 12 $self->tie; # unless $self->dont_tie;
314 12 return $self;
315 }
316  
317 # --- Tie interface
318  
319 sub tie {
320 12 ▶ my ($self) = @_;
Subroutine tie: called CC=1 SCAR=0.0
Pod undocumented
321 12 tie *$self, $self;
322 12 return $self;
323 }
324  
325 sub TIEHANDLE() {
326 12 ▶ return $_[0] if ref $_[0];
Subroutine TIEHANDLE: called CC=2 SCAR=11.6
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 12 0 12
327 0 my $class = shift;
328 0 my $self = bless Symbol::gensym(), $class;
329 0 $self->init(@_);
330 }
331  
332 sub READLINE() {
333 28 ▶ goto &getlines if wantarray;
Subroutine READLINE: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 27 28
334 27 goto &getline;
335 }
336  
337 sub BINMODE {
338 0 ▶ binmode shift()->handle;
Subroutine BINMODE: not called CC=1 SCAR=6.9
339 }
340  
341 sub GETC {
342 0 ▶ die "Arbitrary GETCs not allowed";
Subroutine GETC: not called CC=1 SCAR=6.9
343 }
344  
345 sub PRINT {
346 9 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
347 4 ▶ shift()->print(@_);
Subroutine PRINT: called CC=1 SCAR=0.0
348 }
349  
350 sub PRINTF {
351 9 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
352 0 ▶ shift()->print(sprintf(@_));
Subroutine PRINTF: not called CC=1 SCAR=6.9
353 }
354  
355 sub READ {
356 0 ▶ die "Arbitrary reads not allowed";
Subroutine READ: not called CC=1 SCAR=6.9
357 }
358  
359 sub WRITE {
360 0 ▶ die "Arbitrary writes not allowed";
Subroutine WRITE: not called CC=1 SCAR=6.9
361 }
362  
363 sub SEEK {
364 0 ▶ shift()->seek(@_);
Subroutine SEEK: not called CC=1 SCAR=6.9
365 }
366  
367 sub TELL {
368 0 ▶ shift()->tell;
Subroutine TELL: not called CC=1 SCAR=6.9
369 }
370  
371 sub EOF {
372 0 ▶ shift()->eof;
Subroutine EOF: not called CC=1 SCAR=6.9
373 }
374  
375 sub CLOSE {
376 0 ▶ shift()->close;
Subroutine CLOSE: not called CC=1 SCAR=6.9
377 }
378  
379 sub FILENO {
380 9 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
381 0 ▶ fileno shift()->handle;
Subroutine FILENO: not called CC=1 SCAR=6.9
382 }
383  
384  
385  
386  
387 # --- Functions
388  
389 sub fh_close {
390 13 ▶ my ($fh) = @_;
Subroutine fh_close: called CC=2 SCAR=7.1
Pod undocumented
391 13 ▶ if (UNIVERSAL::isa($fh, 'GLOB')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($fh, 'GLOB')) { } 13 0 13
392 9 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
393 13 $! = 0;
394 13 close $fh;
395 }
396 else {
397 0 $fh->close;
398 }
399 }
400  
401 sub fh_seek {
402 3 ▶ my ($fh, $pos, $whence) = @_;
Subroutine fh_seek: called CC=2 SCAR=7.1
Pod undocumented
403 3 ▶ if (UNIVERSAL::isa($fh, 'GLOB')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($fh, 'GLOB')) { } 3 0 3
404 9 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
405 3 $! = 0;
406 3 seek $fh, $pos, $whence;
407 }
408 else {
409 0 $fh->seek(@_);
410 }
411 }
412  
413 sub fh_tell {
414 3 ▶ my ($fh) = @_;
Subroutine fh_tell: called CC=2 SCAR=7.1
Pod undocumented
415 3 ▶ if (UNIVERSAL::isa($fh, 'GLOB')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($fh, 'GLOB')) { } 3 0 3
416 9 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
417 3 $! = 0;
418 3 tell $fh;
419 }
420 else {
421 0 $fh->tell;
422 }
423 }
424  
425 sub fh_truncate {
426 0 ▶ my ($fh, $length) = @_;
Subroutine fh_truncate: not called CC=2 SCAR=12.2
Pod undocumented
427 0 ▶ if (UNIVERSAL::isa($fh, 'GLOB')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($fh, 'GLOB')) { } 0 0 0
428 9 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
429 0 $! = 0;
430 0 truncate $fh, $length;
431 }
432 else {
433 0 $fh->truncate($length);
434 }
435 }
436  
437 sub fh_eof {
438 3 ▶ my ($fh) = @_;
Subroutine fh_eof: called CC=2 SCAR=7.1
Pod undocumented
439 3 ▶ if (UNIVERSAL::isa($fh, 'GLOB')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($fh, 'GLOB')) { } 3 0 3
440 9 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
441 3 $! = 0;
442 3 eof $fh;
443 }
444 else {
445 0 $fh->eof;
446 }
447 }
448  
449  
450 9 1;
451  
452  
453 =head1 NAME
454  
455 IO::YAML - read and write YAML streams incrementally
456  
457 =head1 SYNOPSIS
458  
459 use IO::YAML;
460
461 $io = IO::YAML->new($path_or_filehandle);
462 $io = IO::YAML->new(
463 'path' => '/path/to/a/file',
464 'auto_load' => $bool,
465 );
466 $io = IO::YAML->new(
467 'handle' => $fh,
468 'mode' => '>', # or 'w'; '<' or 'r'; '>>' or 'a'
469 );
470
471 $io = IO::YAML->new;
472 $io->open($path, '>') or die $!; # Open a stream for writing
473 $io->open($path, '>>') or die $!; # Open a stream for appending
474
475 # Automatically add "...\n" at end of each document written
476 $io->auto_terminate(1);
477
478 print $io $mystring;
479 print $io @myvalues;
480 print $io \@myarray;
481 print $io \%myhash;
482 print $io $myobj;
483
484 $io = IO::YAML->new;
485 $io->open($path, '<') or die $!; # Open a stream for reading
486 while (<$io>) {
487 $data = YAML::Load($_);
488 }
489
490 $io = IO::YAML->new;
491 $io->open($path) or die $!; # Default mode is reading
492 $io->auto_load(1);
493 while (not $io->eof) {
494 $data = <$io>;
495 }
496
497 $io = IO::YAML->new($path_or_handle);
498 $io->auto_load(1);
499 my @values = <$io>; # Roughly equivalent to YAML::LoadFile(...)
500  
501 =head1 DESCRIPTION
502  
503 B<IO::YAML> may be used to read and write YAML streams one C<document> (i.e.,
504 one value) at a time.
505  
506 A YAML stream is a file consisting of a sequence of YAML documents, each of
507 which may (optionally) be terminated by a line consisting solely of three
508 periods (C<...>).
509  
510 The first line of each document must begin with the three-byte sequence C<--->.
511  
512 Here's a simple YAML file consisting of three documents; their values are the
513 string 'foo', an empty array, and a hash with three elements:
514  
515 --- #YAML:1.0 foo
516 --- #YAML:1.0 []
517 --- #YAML:1.0
518 title: Testing 1, 2, 3
519 author: nkuitse
520 date: 2004-03-05
521 ^D
522  
523 (Here, C<^D> indicates the end of the file.)
524  
525 In this next example, the stream consists of a single YAML document whose value
526 is C<undef>:
527  
528 --- ~
529 ^D
530  
531 As this example shows, the first line in each document need not contain the
532 full YAML 1.0 header.
533  
534 =head2 Reading from a YAML stream
535  
536 To read from a YAML stream, you may use the angle-brackets operator (e.g.,
537 E<lt>$fhE<gt>) or the equivalent methods C<getline> or C<read>. Rather than
538 reading a single line, this will read an entire YAML document.
539  
540 while(defined(my $yaml = <$io>)) {
541 my $value = YAML::Load($yaml);
542 ...
543 }
544  
545 The C<YAML::Load> step may be omitted by setting the IO::YAML object's
546 C<auto_load> property to a true value:
547  
548 $io->auto_load(1);
549 while(defined(my $value = <$io>)) {
550 ...
551 }
552  
553 However, this example is complicated by the fact that the value of a YAML
554 document may be undef; the loop as written will terminate when the end of the
555 stream is reached I<or> when an undef value is read.
556  
557 To avoid this problem while still taking advantage of the C<auto_load> property,
558 use C<< $io->eof >> to test for the end of the stream:
559  
560 $io->auto_load(1);
561 while(not $io->eof) {
562 my $value = <$io>;
563 ...
564 }
565  
566 L<IO::YAML|IO::YAML> properly recognizes the document terminator (C<...>).
567 Some versions of L<YAML|YAML> do B<not> recognize it, however; in order to
568 prevent problems when reading YAML streams with auto-loading off,
569 L<IO::YAML|IO::YAML> strips the document terminator line if it is present.
570  
571 =head2 Writing to a YAML stream
572  
573 To print to a YAML stream, call C<print> just as you would with a regular file
574 handle; the value(s) you're printing will be converted to YAML format before
575 being written:
576  
577 $io = IO::YAML->new;
578 $io->open('>file') or die "Couldn't open 'file'";
579 print $io $anything;
580  
581 You can `print' anything that YAML is capable of serializing; an exception will
582 be raised if you attempt to print something that can't be serialized (e.g., a
583 reference to a subroutine).
584  
585 The complication with undef values that affects the reading of a YAML stream
586 is not an issue when writing to a YAML stream.
587  
588 =head2 Terminating YAML documents
589  
590 Documents in a YAML stream may be terminated by a line consisting solely of
591 the string "...". You can use the C<terminate> method to add an explicit
592 document terminator to a YAML stream that you have open for writing (or
593 appending):
594  
595 $io = IO::YAML->new($file_or_handle, '>');
596
597 foreach my $value (@data_values) {
598 print $io $value;
599 $io->terminate;
600 }
601  
602 It's generally safer to have YAML documents terminated automatically:
603  
604 # 1. Set auto_terminate to a true value
605 # a) When creating the object
606 $io = IO::YAML->new(
607 'handle' => $fh,
608 'mode' => '>>',
609 'auto_terminate' => 1,
610 );
611 # or b) At any point thereafter
612 $io = IO::YAML->new(...);
613 $io->auto_terminate(1);
614
615 # 2. Documents are now auto-terminated
616 foreach my $value (@data_values) {
617 print $io $value;
618 # $io->terminate called implicitly
619 }
620  
621 Note that it's not the YAML I<stream> that's terminated; it's the YAML document
622 that was previously written.
623  
624 =head2 Low-level access
625  
626 Sometimes it is helpful to be able to access a YAML stream at a lower level.
627 For example, you may wish to read and write a file consisting of a YAML
628 document (here, serving as a header of sorts) followed by arbitrary text.
629 The C<handle> method may be used to obtain the underlying file handle so
630 that it can be used for this low-level access:
631  
632 # Read header + body
633 $io->auto_load(1);
634 $header = <$io>;
635 $fh = $io->handle;
636 while (<$fh>) {
637 # Process each line after the YAML document
638 }
639
640 # Write header + body
641 $io->auto_terminate(1);
642 print $io $header;
643 $fh = $io->handle;
644 for (@other_stuff_to_write) {
645 print $fh $_;
646 }
647  
648 =head1 METHODS
649  
650 =over 4
651  
652 =item B<new>
653  
654 $io = IO::YAML->new;
655
656 # Concise forms
657 $io = IO::YAML->new("$file"); # Default is read-only
658 $io = IO::YAML->new("<$file"); # Read-only made explicit
659 $io = IO::YAML->new(">$file"); # Read-write (empty header & body)
660 $io = IO::YAML->new($file, '<'); # Or '>', '+<', 'r', etc.
661 $io = IO::YAML->new(\*STDIN);
662 $io = IO::YAML->new(\*STDOUT, '>');
663 $io = IO::YAML->new($anything_that_isa_GLOB);
664
665 # Full-fledged forms
666 $io = IO::YAML->new(
667 'path' => $file, # File will be opened read-only
668 'auto_load' => 1, # Default is 0
669 );
670 $io = IO::YAML->new(
671 'path' => $file, # File will be opened or created
672 'mode' => '>', # Default is '<'; '>>' is also allowed
673 );
674
675 Instantiate an IO::YAML object. An exception is thrown if anything goes
676 wrong.
677  
678 If a path is specified, the file at that path will be opened. Otherwise,
679 you'll have to open it yourself using the C<open()> method.
680  
681 If a path has been specified and the file doesn't already exist, it will be
682 created -- but only if you've specified a mode that permits writing; if you
683 haven't, an exception will be thrown.
684  
685 The following arguments may be specified in the constructor:
686  
687 =over 4
688  
689 =item I<path>
690  
691 Path to a file to create (if it doesn't already exist) and open.
692  
693 =item I<mode>
694  
695 Read/write/append mode for the new file. This must be specified in one
696 of the following forms:
697  
698 =over 4
699  
700 =item E<lt>
701  
702 =item E<gt>
703  
704 =item E<gt>E<gt>
705  
706 =item r
707  
708 =item w
709  
710 =item a
711  
712 Modes that allow for both reading and writing are not allowed, since YAML
713 documents are variable in size.
714  
715 =back
716  
717 B<NOTE:> Numeric modes are not yet implemented.
718  
719 =item I<auto_load>
720  
721 Indicates whether YAML document values should be auto-loaded after being
722 read (see above). The default is B<not> to auto-load values.
723  
724 =item I<auto_terminate>
725  
726 Indicates whether YAML documents should be auto-terminated when they are
727 written (see above). The default is B<not> to auto-terminate documents.
728  
729 =back
730  
731 =item B<open>
732  
733 $io = IO::YAML->new;
734 $io->open("<$file") or die $!;
735 $io->open($file, $mode) or die $!;
736  
737 Open a file with the specified name and mode. You must use this method
738 if the instance was created without a C<path> element (and one has not
739 been assigned using the C<path()> method).
740  
741 Upon failure, sets C<$!> to a meaningful message and returns a false
742 value.
743  
744 The possible modes are as described for B<new>.
745  
746 The C<open()> method may be called repeatedly on the same instance,
747 without having to close it.
748  
749 =item B<close>
750  
751 $io->close or die $!;
752  
753 Close the filehandle.
754  
755 =item B<print>
756  
757 $io->print($data) or die $!;
758  
759 =item B<getline>
760  
761 =item B<getlines>
762  
763 =item B<seek>
764  
765 $io->seek($pos, $whence);
766  
767 Set the IO::YAML file handle's position I<in bytes> within the YAML stream.
768 This will fail unless it moves the position to the beginning of a YAML document
769 or the end of the whole file handle.
770  
771 =item B<tell>
772  
773 $pos = $io->tell;
774  
775 Return the the IO::YAML file handle's position I<in bytes>.
776  
777 =item B<truncate>
778  
779 $io->truncate(0);
780 $io->truncate($io->tell);
781  
782 Truncates the IO::YAML file to the specified length. As illustrated here, this
783 must be either 0 or equal to the filehandle's current position.
784  
785 =item B<eof>
786  
787 if ($io->eof) { ... }
788  
789 Return 1 if the IO::YAML filehandle is at the end of the YAML stream.
790  
791 =back
792  
793 =head1 BUGS
794  
795 Autoflush might not be working.
796  
797 Seeking to the first position beyond the end of the YAML stream should be
798 possible but doesn't currently work.
799  
800 =head1 TO DO
801  
802 Normalize modes passed in the constructor.
803  
804 Implement numeric modes.
805  
806 Add tests for B<seek> and B<tell> methods.
807  
808 Enable seeking to the first byte beyond the end of the YAML stream.
809  
810 Figure out how to allow read-write access and truncate().
811  
812 =head1 SEE ALSO
813  
814 L<YAML|YAML>
815  
816 =head1 AUTHOR
817  
818 Paul Hoffman (nkuitse AT cpan DOT org)
819  
820 =head1 COPYRIGHT
821  
822 Copyright 2004-2007, 2009 Paul M. Hoffman.
823  
824 This is free software, and is made available under the same terms as
825 Perl itself.
826  
↑ Summary Build »