File Coverage

blib/lib/App/Addex/Output/ToFile.pm
Criterion Covered Total %
statement 29 29 100.0
branch 6 6 100.0
condition n/a
subroutine 9 9 100.0
pod 3 3 100.0
total 47 47 100.0


line stmt bran cond sub pod time code
1 4     4   3295 use strict;
  4         9  
  4         146  
2 4     4   21 use warnings;
  4         7  
  4         233  
3              
4             package App::Addex::Output::ToFile;
5             # ABSTRACT: base class for output plugins that write to files
6             $App::Addex::Output::ToFile::VERSION = '0.026';
7 4     4   946 use parent 'App::Addex::Output';
  4         367  
  4         31  
8              
9 4     4   149 use Carp ();
  4         6  
  4         927  
10              
11             #pod =head1 DESCRIPTION
12             #pod
13             #pod This is a base class for output plugins that will write to files. The
14             #pod "filename" configuration parameter must be given, and must be the name of a
15             #pod file to which the user can write.
16             #pod
17             #pod =method new
18             #pod
19             #pod my $addex = App::Addex::Output::Subclass->new(\%arg);
20             #pod
21             #pod This method returns a new outputter. It should be subclassed to provide a
22             #pod C<process_entry> method.
23             #pod
24             #pod Valid arguments are:
25             #pod
26             #pod filename - the file to which to write configuration (required)
27             #pod
28             #pod =cut
29              
30             sub new {
31 6     6 1 3390 my ($class, $arg) = @_;
32              
33 6 100       241 Carp::croak "no filename argument given for $class" unless $arg->{filename};
34              
35 5         45 my $self = $class->SUPER::new;
36              
37 4 100   4   31 open my $fh, '>', $arg->{filename}
  4         8  
  4         34  
  5         475  
38             or Carp::croak "couldn't open output file $arg->{filename}: $!";
39              
40 4     4   27 binmode($fh, ':encoding(utf8)');
  4         7  
  4         23  
  4         6245  
41              
42 4         70676 $self->{fh} = $fh;
43              
44 4         37 return $self;
45             }
46              
47             #pod =method output
48             #pod
49             #pod $outputter->output($string);
50             #pod
51             #pod This method appends the given string to the output file, adding a newline.
52             #pod
53             #pod =cut
54              
55             sub output {
56 51     51 1 133 my ($self, $line) = @_;
57              
58 51 100       62 print { $self->{fh} } "$line\n"
  51         451  
59             or Carp::croak "couldn't write to output file: $!";
60             }
61              
62             sub finalize {
63             # We'll just delete this ref and let garbage collection cause closing if
64             # needed. -- rjbs, 2007-11-05
65 3     3 1 107 delete $_[0]->{fh};
66             }
67              
68             1;
69              
70             __END__
71              
72             =pod
73              
74             =encoding UTF-8
75              
76             =head1 NAME
77              
78             App::Addex::Output::ToFile - base class for output plugins that write to files
79              
80             =head1 VERSION
81              
82             version 0.026
83              
84             =head1 DESCRIPTION
85              
86             This is a base class for output plugins that will write to files. The
87             "filename" configuration parameter must be given, and must be the name of a
88             file to which the user can write.
89              
90             =head1 METHODS
91              
92             =head2 new
93              
94             my $addex = App::Addex::Output::Subclass->new(\%arg);
95              
96             This method returns a new outputter. It should be subclassed to provide a
97             C<process_entry> method.
98              
99             Valid arguments are:
100              
101             filename - the file to which to write configuration (required)
102              
103             =head2 output
104              
105             $outputter->output($string);
106              
107             This method appends the given string to the output file, adding a newline.
108              
109             =head1 AUTHOR
110              
111             Ricardo SIGNES <rjbs@cpan.org>
112              
113             =head1 COPYRIGHT AND LICENSE
114              
115             This software is copyright (c) 2006 by Ricardo SIGNES.
116              
117             This is free software; you can redistribute it and/or modify it under
118             the same terms as the Perl 5 programming language system itself.
119              
120             =cut