| 1 | #!perl | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # ########################################################################## # | ||||||||||||||
| 4 | # Title: Write stream to tabular file | ||||||||||||||
| 5 | # Creation date: 2007-03-05 | ||||||||||||||
| 6 | # Author: Michael Zedeler | ||||||||||||||
| 7 | # Description: Writes data stream to file | ||||||||||||||
| 8 | # Data Stream class | ||||||||||||||
| 9 | # Transformer | ||||||||||||||
| 10 | # File: $Source: /data/cvs/lib/DSlib/lib/DS/Transformer/TabFileWriter.pm,v $ | ||||||||||||||
| 11 | # Repository: kronhjorten | ||||||||||||||
| 12 | # State: $State: Exp $ | ||||||||||||||
| 13 | # Documentation: inline | ||||||||||||||
| 14 | # Recepient: - | ||||||||||||||
| 15 | # ########################################################################## # | ||||||||||||||
| 16 | |||||||||||||||
| 17 | package DS::Transformer::TabFileWriter; | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 1 | ▶ | use base qw{ DS::Transformer::TabStreamWriter }; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | |||||||||||||||
| 21 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 1 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | 1 | ▶ | use Carp::Assert; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 1 | ▶ | use IO::File; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | |||||||||||||||
| 26 | 1 | our ($VERSION) = $DS::VERSION; | |||||||||||||
| 27 | 1 | our ($REVISION) = '$Revision: 1.1 $' =~ /(\d+\.\d+)/; | |||||||||||||
| 28 | |||||||||||||||
| 29 | |||||||||||||||
| 30 | 1 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.9
Pod
documented
| |||||||||||||||
| 31 | 1 | my( $class, $filename, $field_order, $source, $target ) = @_; | |||||||||||||
| 32 | |||||||||||||||
| 33 | 1 | my $self = undef; | |||||||||||||
| 34 | 1 | assert(ref($filename) eq '', "Must be scalar but is " . ref( $filename )); | |||||||||||||
| 35 | 1 | my $fh; | |||||||||||||
| 36 | 1 | ▶ | if( not defined( $filename ) ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 37 | 0 | $fh = new_tmpfile IO::File; | |||||||||||||
| 38 | } elsif( not -f $filename ) { | ||||||||||||||
| 39 | 1 | $fh = new IO::File; | |||||||||||||
| 40 | 1 | $fh->open( $filename, ">" ); | |||||||||||||
| 41 | } else { | ||||||||||||||
| 42 | 0 | croak("The file $filename already exists."); | |||||||||||||
| 43 | } | ||||||||||||||
| 44 | |||||||||||||||
| 45 | 1 | ▶ | if( $fh ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | 1 | $self = $class->SUPER::new( $fh, $field_order, $source, $target ); | |||||||||||||
| 47 | 1 | $self->{filename} = $filename; | |||||||||||||
| 48 | } else { | ||||||||||||||
| 49 | 0 | $self = undef; | |||||||||||||
| 50 | } | ||||||||||||||
| 51 | |||||||||||||||
| 52 | 1 | return $self; | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | |||||||||||||||
| 55 | 12 | ▶ | sub process { | ||||||||||||
|
Subroutine
process: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 56 | 12 | my( $self, $row ) = @_; | |||||||||||||
| 57 | |||||||||||||||
| 58 | 12 | my $result; | |||||||||||||
| 59 | |||||||||||||||
| 60 | # TODO (V3) Writing header this late is not a good thing. Move to post-attach, pre-process stage. | ||||||||||||||
| 61 | 12 | ▶ | unless( $self->{header_written} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | 1 | $self->{header_written} = 1; | |||||||||||||
| 63 | 1 | $self->write_header; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 12 | $result = $self->SUPER::process( $row ); | |||||||||||||
| 67 | 12 | ▶ | $self->{fh}->close() unless $row; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | |||||||||||||||
| 69 | 12 | return $result; | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | 1 | 1; | |||||||||||||