The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

CSV::Reader - CSV reader class

DESCRIPTION

Simple CSV reader class that uses Text::CSV internally. The CSV files are expected to have a header row of column names. This was designed with the idea of using an iterator interface, but Perl does not support interators (nor interfaces) yet :(

SYNOPSIS

        use CSV::Reader ();
        use open OUT => ':locale'; # optional; make perl aware of your terminal's encoding

        # Create reader from file name:
        my $reader = new CSV::Reader('/path/to/file.csv');

        # Create reader from a file handle (GLOB):
        open(my $h, '<', $filename) || die("Failed to open $filename: $!");
        # or preferred method that can handle files having a UTF-8 BOM:
        open(my $h, '<:via(File::BOM)', $filename) || die("Failed to open $filename: $!");
        my $reader = new CSV::Reader($h);

        # Create reader from an IO::Handle based object:
        my $io = IO::File->new(); # subclass of IO::Handle
        $io->open($filename, '<:via(File::BOM)') || die("Failed to open $filename: $!");
        my $reader = new CSV::Reader($io);

        # Show the field names found in the header row:

        print Dumper([$reader->fieldNames()]);

        # Iterate over the data rows:
        while (my $row = $reader->nextRow()) {
                # It's recommended to validate the $row hashref first with something such as Params::Validate.
                # Now do whatever you want with the (validated) row hashref...
                require Data::Dumper; local $Data::Dumper::Terse = 1;
                print Data::Dumper::Dumper($row);
        }

PUBLIC STATIC METHODS

new($file, %options)

Constructor.

$file can be a string file name, an open file handle (GLOB), or an IO::Handle based object (e.g. IO::File or IO::Scalar). If a string file name is given, then the file is opened via File::BOM.

The following %options are supported:

        - debug: boolean, if true, then debug messages are emitted using warn().
        - field_aliases: hashref of case insensitive alias (in file) => real name (as expected in code) pairs.
        - field_normalizer: optional callback that receives a field name by reference to normalize (e.g. make lowercase).
        - include_fields: optional arrayref of field names to include. If given, then all other field names are excluded.
        - delimiter: string, default ','
        - enclosure: string, default '"'
        - escape: string, default backslash

DESTROY

Closes the private file handle, if any.

PROTECTED OBJECT METHODS

_read()

Reads the next CSV data row and sets internal variables.

PUBLIC OBJECT METHODS

fieldNames()

Returns the field names as an array.

current()

Returns the current row.

linenum()

Returns the current row index.

nextRow()

Reads the next row.

eof()

Returns boolean

rewind()

Rewinds the file handle.

SEE ALSO

Text::CSV used by this class internally.

AUTHOR

Craig Manley

COPYRIGHT

Copyright (C) 2020 Craig Manley. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.