CSV-Reader

 view release on metacpan or  search on metacpan

META.json  view on Meta::CPAN

            "Test::More" : "0"
         }
      },
      "configure" : {
         "requires" : {
            "ExtUtils::MakeMaker" : "0"
         }
      },
      "runtime" : {
         "requires" : {
            "File::BOM" : "0",
            "Test::More" : "0",
            "Text::CSV" : "0",
            "Tie::IxHash" : "0",
            "perl" : "5.01"
         }
      }
   },
   "release_status" : "stable",
   "resources" : {
      "bugtracker" : {

META.yml  view on Meta::CPAN

license: perl
meta-spec:
  url: http://module-build.sourceforge.net/META-spec-v1.4.html
  version: '1.4'
name: CSV-Reader
no_index:
  directory:
    - t
    - inc
requires:
  File::BOM: '0'
  Test::More: '0'
  Text::CSV: '0'
  Tie::IxHash: '0'
  perl: '5.01'
resources:
  bugtracker: https://github.com/cmanley/p5-CSV-Reader/issues
  repository: https://github.com/cmanley/p5-CSV-Reader.git
version: 1.12
x_serialization_backend: 'CPAN::Meta::YAML version 0.018'

Makefile.PL  view on Meta::CPAN

	'NAME' => 'CSV::Reader',
	'AUTHOR' => 'Craig Manley',
	'ABSTRACT' => 'Simple CSV reader class that uses Text::CSV internally.',
	#'MIN_PERL_VERSION' => '5.013000',
	'VERSION_FROM' => 'lib/CSV/Reader.pm',
	'BUILD_REQUIRES' => {
		'ExtUtils::MakeMaker'	=> 6.48,
		'Test::More'			=> 0,
	},
	'PREREQ_PM' =>	{
		'File::BOM'		=> 0,
		'Text::CSV'		=> 0,
		'Tie::IxHash'	=> 0,
		'Test::More'	=> 0,
	},
	'MIN_PERL_VERSION'	=> 5.010000,
	'LICENSE' => 'perl_5',
	'dist' => {COMPRESS => 'gzip', SUFFIX => 'tar.gz'},
	'META_MERGE' => {
		'meta-spec' => { 'version' => 2 },
		'resources' => {

README.md  view on Meta::CPAN

--------
```perl
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);

# Create reader with advanced options:
my $reader = new CSV::Reader('/path/to/file.csv',
	'delimiter' => ';',
	'enclosure' => '',
	'field_normalizer' => sub {
		my $nameref = shift;
		$$nameref = lc($$nameref);	# lowercase
		$$nameref =~ s/\s/_/g;	# whitespace to underscore

README.md  view on Meta::CPAN

```

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```: callback that receives a field name by reference to normalize (e.g. make lowercase).
- ```include_fields```: 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

README.md  view on Meta::CPAN

### eof()

Returns boolean

### rewind()

Rewinds the file handle.

Requirements
------------
- File::BOM (recommended; not required)
- Params::Validate
- Text::CSV
- Tie::IxHash

Installation using cpan
-----------------------
CSV::Reader may be installed through the CPAN shell in the usual manner:
```
# perl -MCPAN -e 'install CSV::Reader'
```

lib/CSV/Reader.pm  view on Meta::CPAN

=head1 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);

	# Create reader with advanced options:
	my $reader = new CSV::Reader('/path/to/file.csv',
		'delimiter' => ';',
		'enclosure' => '',
		'field_normalizer' => sub {
			my $nameref = shift;
			$$nameref = lc($$nameref);	# lowercase
			$$nameref =~ s/\s/_/g;	# whitespace to underscore

lib/CSV/Reader.pm  view on Meta::CPAN

		print Data::Dumper::Dumper($row);
	}

=head1 PUBLIC STATIC METHODS

=head2 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: callback that receives a field name by reference to normalize (e.g. make lowercase).
	- include_fields: 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

lib/CSV/Reader.pm  view on Meta::CPAN

	if (ref($file)) {
		unless ((ref($file) eq 'GLOB') || UNIVERSAL::isa($file, 'IO::Handle')) {
			croak(ref($file) . ' is not a legal file argument type');
		}
		$self->{'h'} = $file;
		$self->{'own_h'} = 0;
	}
	else {
		my $h;
		eval {
			require File::BOM;
		};
		my $mode = $@ ? '<' : '<:via(File::BOM)';
		$options{'debug'} && warn(__PACKAGE__ . "::new file open mode is $mode\n");
		open($h, $mode, $file) || croak('Failed to open "' . $file . '" for reading using mode "' . $mode . '": ' . $!);
		$self->{'h'} = $h;
		$self->{'own_h'} = 1;
	}

	# Get the options.
	my %opt_field_aliases;
	my $opt_field_normalizer;
	my %opt_include_fields;

t/reader.t  view on Meta::CPAN

			my $addr = $row->{'Address Code 2'};
			if (defined($addr)) {
				ok(utf8::is_utf8($addr), "$name: Address \"$addr\" is flagged as UTF-8");
				is($addr, 'Déjà vu straat', "$name: UTF-8 encoding is OK after parsing");
			}
		},
	},
	#'file handle' => {
	#	'construct' => sub {
	#		open(my $h, '<', $csvfile) || die("Failed to open $csvfile: $!");
	#		seek($h, 3, 0);	# skip passed BOM
	#		return $class->new($h, %default_options);
	#	},
	#},
	'file handle via File::BOM' => {
		'construct' => sub {
			my $h;
			open($h, '<:via(File::BOM)', $csvfile) || die("Failed to open $csvfile: $!");
			return $class->new($h, %default_options);
		},
		'requires' => sub {
			require File::BOM;
		},
	},
	'IO::File (IO::Handle)' => {
		'construct' => sub {
			require IO::File;
			my $io = IO::File->new(); # subclass of IO::Handle
			$io->open($csvfile, '<:via(File::BOM)') || die("Failed to open $csvfile: $!");
			return $class->new($io, %default_options);
		},
		'requires' => sub {
			require File::BOM;
			require IO::File;
		},
	},
	'IO::Scalar (IO::Handle)' => {
		'construct' => sub {
			my $data = '';
			my $h;
			open($h, '<:via(File::BOM)', $csvfile) || die("Failed to open $csvfile: $!");
			while (my $line = <$h>) {
				$data .= $line;
			}
			close($h);
			my $io = new IO::Scalar(\$data); # subclass of IO::Handle
			return $class->new($io, %default_options);
		},
		'extra row tests' => sub {
			my $name = shift;
			my $o = shift;
			my $row = shift;
			my $addr = $row->{'Address Code 2'};
			if (defined($addr)) {
				ok(utf8::is_utf8($addr), "$name: Address \"$addr\" is flagged as UTF-8");
				is($addr, 'Déjà vu straat', "$name: UTF-8 encoding is OK after parsing");
			}
		},
		'requires' => sub {
			require File::BOM;
			require IO::Scalar;
		},
	},
	'complex' => {
		'construct' => sub {
			my $h;
			open($h, '<:via(File::BOM)', $csvfile) || die($!);
			my %opts = (
				%default_options,
			);
			return $class->new($h, %opts);
		},
		'extra row tests' => sub {
			my $name = shift;
			my $o = shift;
			my $row = shift;
			my $addr = $row->{'Address Code 2'};
			if (defined($addr)) {
				ok(utf8::is_utf8($addr), "$name: Address \"$addr\" is flagged as UTF-8");
				is($addr, 'Déjà vu straat', "$name: UTF-8 encoding is OK after parsing");
			}
		},
		'requires' => sub {
			require File::BOM;
		},
	},
	'include_fields' =>	{
		'construct' => sub {
			return $class->new($csvfile, %default_options,
				'include_fields' => [
					'postcode',
					'Address Code 2',
				],
			);



( run in 1.394 second using v1.01-cache-2.11-cpan-e9daa2b36ef )