| 1 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | package App::Addex::Output::ToFile 0.027; | ||||||||||
| 5 | # ABSTRACT: base class for output plugins that write to files | ||||||||||
| 6 | |||||||||||
| 7 | 4 | ▶ | use parent 'App::Addex::Output'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 4 | ▶ | use Carp (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 6 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 31 | 6 | my ($class, $arg) = @_; | |||||||||
| 32 | |||||||||||
| 33 | 6 | ▶ | Carp::croak "no filename argument given for $class" unless $arg->{filename}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | |||||||||||
| 35 | 5 | my $self = $class->SUPER::new; | |||||||||
| 36 | |||||||||||
| 37 | open my $fh, '>', $arg->{filename} | ||||||||||
| 38 | 5 | ▶ | or Carp::croak "couldn't open output file $arg->{filename}: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | |||||||||||
| 40 | 4 | binmode($fh, ':encoding(utf8)'); | |||||||||
| 41 | |||||||||||
| 42 | 4 | $self->{fh} = $fh; | |||||||||
| 43 | |||||||||||
| 44 | 4 | 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 | 51 | ▶ | sub output { | ||||||||
|
Subroutine
output: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 56 | 51 | my ($self, $line) = @_; | |||||||||
| 57 | |||||||||||
| 58 | 51 | ▶ | print { $self->{fh} } "$line\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | or Carp::croak "couldn't write to output file: $!"; | ||||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 3 | ▶ | sub finalize { | ||||||||
|
Subroutine
finalize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 63 | # We'll just delete this ref and let garbage collection cause closing if | ||||||||||
| 64 | # needed. -- rjbs, 2007-11-05 | ||||||||||
| 65 | 3 | delete $_[0]->{fh}; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 4 | 1; | |||||||||
| 69 | |||||||||||
| 70 | __END__ | ||||||||||