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   2167 use strict;
  4         9  
  4         119  
2 4     4   21 use warnings;
  4         6  
  4         165  
3              
4             package App::Addex::Output::ToFile 0.027;
5             # ABSTRACT: base class for output plugins that write to files
6              
7 4     4   465 use parent 'App::Addex::Output';
  4         326  
  4         25  
8              
9 4     4   165 use Carp ();
  4         9  
  4         941  
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 1738 my ($class, $arg) = @_;
32              
33 6 100       213 Carp::croak "no filename argument given for $class" unless $arg->{filename};
34              
35 5         26 my $self = $class->SUPER::new;
36              
37             open my $fh, '>', $arg->{filename}
38 4 100   4   26 or Carp::croak "couldn't open output file $arg->{filename}: $!";
  4         9  
  4         30  
  5         291  
39              
40 4     4   25 binmode($fh, ':encoding(utf8)');
  4         6  
  4         19  
  4         3511  
41              
42 4         50012 $self->{fh} = $fh;
43              
44 4         39 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 109 my ($self, $line) = @_;
57              
58 51 100       61 print { $self->{fh} } "$line\n"
  51         274  
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 86 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.027
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 PERL VERSION SUPPORT
91              
92             This module has the same support period as perl itself: it supports the two
93             most recent versions of perl. (That is, if the most recently released version
94             is v5.40, then this module should work on both v5.40 and v5.38.)
95              
96             Although it may work on older versions of perl, no guarantee is made that the
97             minimum required version will not be increased. The version may be increased
98             for any reason, and there is no promise that patches will be accepted to lower
99             the minimum required perl.
100              
101             =head1 METHODS
102              
103             =head2 new
104              
105             my $addex = App::Addex::Output::Subclass->new(\%arg);
106              
107             This method returns a new outputter. It should be subclassed to provide a
108             C<process_entry> method.
109              
110             Valid arguments are:
111              
112             filename - the file to which to write configuration (required)
113              
114             =head2 output
115              
116             $outputter->output($string);
117              
118             This method appends the given string to the output file, adding a newline.
119              
120             =head1 AUTHOR
121              
122             Ricardo SIGNES <rjbs@semiotic.systems>
123              
124             =head1 COPYRIGHT AND LICENSE
125              
126             This software is copyright (c) 2006 by Ricardo SIGNES.
127              
128             This is free software; you can redistribute it and/or modify it under
129             the same terms as the Perl 5 programming language system itself.
130              
131             =cut