| 1 | package Data::Package::CSV; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Data::Package::CSV - A Data::Package class for CSV data using Parse::CSV | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | The B<Data::Package::CSV> package provides a subclass of | ||
| 12 | L<Data::Package::File> that provides data from a CSV file by integrating | ||
| 13 | with L<Parse::CSV>. | ||
| 14 | |||
| 15 | =head1 METHODS | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | 2 | ▶ | use 5.005; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 2 | ▶ | use base 'Data::Package::File'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | use Parse::CSV (); | ||
| 23 | |||
| 24 | use vars qw{$VERSION}; | ||
| 25 | BEGIN { | ||
| 26 | $VERSION = '1.01'; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub import { | ||
| 30 | return 1 if $_[0] eq __PACKAGE__; | ||
| 31 | return shift->SUPER::import(@_); | ||
| 32 | } | ||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | |||
| 38 | ##################################################################### | ||
| 39 | # Data::Package::CSV Methods | ||
| 40 | |||
| 41 | =pod | ||
| 42 | |||
| 43 | =head2 csv_options | ||
| 44 | |||
| 45 | The B<cvs_options> method is the most direct method, with full control | ||
| 46 | over the creation of the L<Parse::CSV> object. If a fully compliant options | ||
| 47 | hash is returned (as a list) then no other methods need to be defined. | ||
| 48 | |||
| 49 | The list returned by the B<cvs_options> method will be passed directly to | ||
| 50 | the L<Parse::CSV> constructor. Read the documentation for L<Parse::CSV> for | ||
| 51 | more details on what you should return to match your data. | ||
| 52 | |||
| 53 | By default, the null list is return, specifying entirely default options | ||
| 54 | to the L<Parse::CSV> constructor (array mode) and not specifying any filters | ||
| 55 | or parsing variations. | ||
| 56 | |||
| 57 | If it list that is returned does not have either a data source (either a | ||
| 58 | C<handle> param or C<file> param) then the C<file> method for the parent | ||
| 59 | class will be called to locate a file. | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub csv_options { | ||
| 64 | return (); | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | |||
| 71 | ##################################################################### | ||
| 72 | # Data::Package Methods | ||
| 73 | |||
| 74 | sub _provides { | ||
| 75 | my @provides = shift->SUPER::_provides(@_); | ||
| 76 | return ( 'Parse::CSV', @provides ); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub __as_Parse_CSV { | ||
| 80 | my $class = ref($_[0]) || $_[0]; | ||
| 81 | |||
| 82 | # Get the main options | ||
| 83 | my %options = $class->csv_options; | ||
| 84 | unless ( $options{handle} or $options{file} ) { | ||
| 85 | delete $options{file}; | ||
| 86 | delete $options{handle}; | ||
| 87 | |||
| 88 | # Locate the data | ||
| 89 | my $file = $class->file; | ||
| 90 | if ( $file ) { | ||
| 91 | $options{file} = $class->file; | ||
| 92 | delete $options{handle}; | ||
| 93 | } else { | ||
| 94 | die "No CSV file found for $class"; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | # Create the parser object | ||
| 99 | my $parse_csv = Parse::CSV->new( %options ); | ||
| 100 | unless ( $parse_csv ) { | ||
| 101 | die "Failed to create Parse::CSV object for $class"; | ||
| 102 | } | ||
| 103 | |||
| 104 | return $parse_csv; | ||
| 105 | } | ||
| 106 | |||
| 107 | 1; | ||
| 108 | |||
| 109 | =pod | ||
| 110 | |||
| 111 | =head1 SUPPORT | ||
| 112 | |||
| 113 | Bugs should always be submitted via the CPAN bug tracker. | ||
| 114 | |||
| 115 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Package-CSV> | ||
| 116 | |||
| 117 | For other issues, contact the maintainer | ||
| 118 | |||
| 119 | =head1 AUTHOR | ||
| 120 | |||
| 121 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 122 | |||
| 123 | =head1 COPYRIGHT | ||
| 124 | |||
| 125 | Copyright 2007 Adam Kennedy. | ||
| 126 | |||
| 127 | This program is free software; you can redistribute | ||
| 128 | it and/or modify it under the same terms as Perl itself. | ||
| 129 | |||
| 130 | The full text of the license can be found in the | ||
| 131 | LICENSE file included with this module. | ||
| 132 | |||
| 133 | =cut | ||