File Coverage

blib/lib/YAML/PP/Writer/File.pm
Criterion Covered Total %
statement 31 31 100.0
branch 6 6 100.0
condition n/a
subroutine 9 9 100.0
pod 3 3 100.0
total 49 49 100.0


line stmt bran cond sub pod time code
1 43     43   147456 use strict;
  43         63  
  43         1514  
2 43     43   151 use warnings;
  43         181  
  43         3053  
3             package YAML::PP::Writer::File;
4              
5             our $VERSION = 'v0.39.0'; # VERSION
6              
7 43     43   238 use Scalar::Util qw/ openhandle /;
  43         72  
  43         2086  
8              
9 43     43   216 use base qw/ YAML::PP::Writer /;
  43         88  
  43         5962  
10              
11 43     43   314 use Carp qw/ croak /;
  43         58  
  43         11426  
12              
13             sub _open_handle {
14 6     6   8 my ($self) = @_;
15 6 100       31 if (openhandle($self->{output})) {
16 1         2 $self->{filehandle} = $self->{output};
17 1         3 return $self->{output};
18             }
19             open my $fh, '>:encoding(UTF-8)', $self->{output}
20 5 100       1559 or croak "Could not open '$self->{output}' for writing: $!";
21 4         283 $self->{filehandle} = $fh;
22 4         15 return $fh;
23             }
24              
25             sub write {
26 30     30 1 42 my ($self, $line) = @_;
27 30         34 my $fh = $self->{filehandle};
28 30         114 print $fh $line;
29             }
30              
31             sub init {
32 6     6 1 10 my ($self) = @_;
33 6         12 my $fh = $self->_open_handle;
34             }
35              
36             sub finish {
37 5     5 1 7 my ($self) = @_;
38 5 100       18 if (openhandle($self->{output})) {
39             # Original argument was a file handle, so the caller needs
40             # to close it
41 1         2 return;
42             }
43 4         326 close $self->{filehandle};
44             }
45              
46             1;
47              
48             __END__
49              
50             =pod
51              
52             =encoding utf-8
53              
54             =head1 NAME
55              
56             YAML::PP::Writer::File - Write YAML output to file or file handle
57              
58             =head1 SYNOPSIS
59              
60             my $writer = YAML::PP::Writer::File->new(output => $file);
61              
62             =head1 DESCRIPTION
63              
64             The L<YAML::PP::Emitter> sends its output to the writer.
65              
66             You can use your own writer. if you want to send the YAML output to
67             somewhere else. See t/44.writer.t for an example.
68              
69             =head1 METHODS
70              
71             =over
72              
73             =item new
74              
75             my $writer = YAML::PP::Writer::File->new(output => $file);
76             my $writer = YAML::PP::Writer::File->new(output => $filehandle);
77              
78             Constructor.
79              
80             =item write
81              
82             $writer->write('- ');
83              
84             =item init
85              
86             $writer->init;
87              
88             Initialize
89              
90             =item finish
91              
92             $writer->finish;
93              
94             Gets called when the output ends. If The argument was a filename, the
95             filehandle will be closed. If the argument was a filehandle, the caller needs to
96             close it.
97              
98             =item output, set_output
99              
100             Getter/setter for the YAML output
101              
102             =back
103              
104             =cut