Cover V11, I10
oct2002.tar

Listing 3 CSV2db::Util

package CSV2db::Util;

use strict;
use vars qw($VERSION @ISA @EXPORT_OK);
use IO::File;
require Exporter;

$VERSION = substr q$Revision: 1.0 $, 10;
@ISA = qw(Exporter);
@EXPORT_OK = qw(fixlineends trim);

### Some utility functions that might be useful as preprocess and
### per-field processing functions.  If so, you'll want their exported
### names.  THESE ARE NOT CLASS METHODS, though they have a classy
### namespace.

# Preprocess function: read from one fh to another, fixing lineends
# along the way.  CAREFUL: this _might_ read in the whole input file
# in a single slurp.
sub fixlineends {
    my($in, $out) = @_;

    while (<$in>) {
    s/\015\012/\n/g;  # DOS/Windows
    s/\015/\n/g;      # Macintosh
    s/\012/\n/g;      # Unix (we might not be on Unix, eh?)
    $out->print($_);
    }
}

# Should do nothing, can be used as a test preprocessor.
# Put a gross substitute in to see it work (e.g. s/e/#/g;)
sub testcopy {
    my($in, $out) = @_;
    while (<$in>) {
    $out->print($_);
    }
}

# General or per-field processing function to trim leading and
# trailing whitespace.  Expects a single string, returns the trimmed
# string.
sub trim {
    my $s = shift @_;
    $s =~ s/^\s*//;
    $s =~ s/\s*$//;
    $s;
}


1;