| 1 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | package Parse::CSV; | ||||||||||||||||||||||||||||
| 3 | 3 | $Parse::CSV::VERSION = '2.07'; | |||||||||||||||||||||||||||
| 4 | =pod | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | Parse::CSV - Highly flexible CSV parser for large files | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 VERSION | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | version 2.07 | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Simple headerless comma-separated column parser | ||||||||||||||||||||||||||||
| 17 | my $simple = Parse::CSV->new( | ||||||||||||||||||||||||||||
| 18 | file => 'file.csv', | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | while ( my $array_ref = $simple->fetch ) { | ||||||||||||||||||||||||||||
| 22 | # Do something... | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | ... or a more complex example... | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Parse a colon-separated variables file from a handle as a hash | ||||||||||||||||||||||||||||
| 28 | # based on headers from the first line. | ||||||||||||||||||||||||||||
| 29 | # Then filter, so we emit objects rather than the plain hash. | ||||||||||||||||||||||||||||
| 30 | my $objects = Parse::CSV->new( | ||||||||||||||||||||||||||||
| 31 | handle => $io_handle, | ||||||||||||||||||||||||||||
| 32 | sep_char => ';', | ||||||||||||||||||||||||||||
| 33 | names => 1, | ||||||||||||||||||||||||||||
| 34 | filter => sub { My::Object->new( $_ ) }, | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | while ( my $object = $objects->fetch ) { | ||||||||||||||||||||||||||||
| 38 | $object->do_something; | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | Surely the CPAN doesn't need yet another CSV parsing module. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | L<Text::CSV_XS> is the standard parser for CSV files. It is fast as hell, | ||||||||||||||||||||||||||||
| 46 | but unfortunately it can be a bit verbose to use. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | A number of other modules have attempted to put usability wrappers around | ||||||||||||||||||||||||||||
| 49 | this venerable module, but they have all focused on parsing the entire | ||||||||||||||||||||||||||||
| 50 | file into memory at once. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | This method is fine unless your CSV files start to get large. Once that | ||||||||||||||||||||||||||||
| 53 | happens, the only existing option is to fall back on the relatively slow | ||||||||||||||||||||||||||||
| 54 | and heavyweight L<XML::SAXDriver::CSV> module. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | L<Parse::CSV> fills this functionality gap. It provides a flexible | ||||||||||||||||||||||||||||
| 57 | and light-weight streaming parser for large, extremely large, or | ||||||||||||||||||||||||||||
| 58 | arbitrarily large CSV files. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =head2 Main Features | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | B<Stream-Based Parser> - All parsing a line at a time. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | B<Array Mode> - Parsing can be done in simple array mode, returning | ||||||||||||||||||||||||||||
| 65 | a reference to an array if the columns are not named. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | B<Hash Mode> - Parsing can be done in hash mode, putting the data into | ||||||||||||||||||||||||||||
| 68 | a hash and returning a reference to it. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | B<Filter Capability> - All items returned can be passed through a | ||||||||||||||||||||||||||||
| 71 | custom filter. This filter can either modify the data on the fly, | ||||||||||||||||||||||||||||
| 72 | or drop records you don't need. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head2 Writing Filters | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | A L<Parse::CSV> filter is a subroutine reference that is passed the | ||||||||||||||||||||||||||||
| 77 | original record as C<$_> (not as a function argument), and should | ||||||||||||||||||||||||||||
| 78 | C<return> the alternative or modified record. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | A no-op filter (does not modify or drop any records) would look like the | ||||||||||||||||||||||||||||
| 81 | following. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub { $_ } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | A filter that reversed the order of the columns (assuming the parser | ||||||||||||||||||||||||||||
| 86 | is in array mode) might look like the following. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub { [ reverse @$_ ] } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | To drop the record, return C<undef> from the filter. The | ||||||||||||||||||||||||||||
| 91 | parser will then keep pulling and parsing new records until one | ||||||||||||||||||||||||||||
| 92 | passes the filter. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Only keep records where the 'foo' field is true | ||||||||||||||||||||||||||||
| 95 | sub { $_->{foo} ? $_ : undef } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | To signal an error, throw an exception | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | sub { | ||||||||||||||||||||||||||||
| 100 | $_->{foo} =~ /bar/ or die "Assumption failed"; | ||||||||||||||||||||||||||||
| 101 | return $_; | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Feel free to modify C<$_> as a side-effect of your filter routine - | ||||||||||||||||||||||||||||
| 105 | this will have no effect on anything. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head1 METHODS | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 3 | ▶ | use 5.005; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 112 | 3 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 113 | 3 | ▶ | use IO::File 1.13 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 114 | 3 | ▶ | use Text::CSV_XS 1.22 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | 3 | ▶ | use Params::Util 1.00 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | ##################################################################### | ||||||||||||||||||||||||||||
| 119 | # Constructor | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =pod | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head2 new | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | The C<new> constructor creates and initialises a new CSV parser. It | ||||||||||||||||||||||||||||
| 126 | returns a new L<Parse::CSV> object, or throws an exception (dies) on | ||||||||||||||||||||||||||||
| 127 | error. It accepts a number of params: | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =over 4 | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =item C<file> | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =item C<handle> | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | To specify the CSV data source, provide either the C<file> | ||||||||||||||||||||||||||||
| 136 | param, which should be the name of the file to read, or the C<handle> | ||||||||||||||||||||||||||||
| 137 | param, which should be a file handle to read instead. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =item C<csv_attr> | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Any parameter for L<Text::CSV_XS>'s constructor can also be provided | ||||||||||||||||||||||||||||
| 142 | to this C<new> method, and they will be passed on to it. | ||||||||||||||||||||||||||||
| 143 | Alternatively, they can be passed as a single C<HASH> reference as the | ||||||||||||||||||||||||||||
| 144 | C<csv_attr> param. For example: | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | $parser = Parse::CSV->new( | ||||||||||||||||||||||||||||
| 147 | file => 'file.csv', | ||||||||||||||||||||||||||||
| 148 | csv_attr => { | ||||||||||||||||||||||||||||
| 149 | sep_char => ';', | ||||||||||||||||||||||||||||
| 150 | quote_char => "'", | ||||||||||||||||||||||||||||
| 151 | }, | ||||||||||||||||||||||||||||
| 152 | ); | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =item C<names> | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | An optional C<names> param can be provided, which should either be an | ||||||||||||||||||||||||||||
| 157 | array reference containing the names of the columns: | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | $parser = Parse::CSV->new( | ||||||||||||||||||||||||||||
| 160 | file => 'file.csv', | ||||||||||||||||||||||||||||
| 161 | names => [ 'col1', 'col2', 'col3' ], | ||||||||||||||||||||||||||||
| 162 | ); | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | or a true value that's not a reference, indicating that the column | ||||||||||||||||||||||||||||
| 165 | names will be read from the first line of the input: | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | $parser = Parse::CSV->new( | ||||||||||||||||||||||||||||
| 168 | file => 'file.csv', | ||||||||||||||||||||||||||||
| 169 | names => 1, | ||||||||||||||||||||||||||||
| 170 | ); | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | If the C<names> param is provided, the parser will map each line to a | ||||||||||||||||||||||||||||
| 173 | hash where the keys are the field names provided, and the values are the | ||||||||||||||||||||||||||||
| 174 | values found in the CSV file. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | If the C<names> param is B<not> provided, the parser will return simple | ||||||||||||||||||||||||||||
| 177 | array references of the columns, treating them just like all the other | ||||||||||||||||||||||||||||
| 178 | rows in the file. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | If your CSV file has (or might have) a <Byte-Order Mark|https://en.wikipedia.org/wiki/Byte_order_mark>, | ||||||||||||||||||||||||||||
| 181 | you must use the C<names> functionality, because this lets us call the C<header> | ||||||||||||||||||||||||||||
| 182 | method of C<Text::CSV_XS>, which is the only place the BOM is handled | ||||||||||||||||||||||||||||
| 183 | in that module. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =item C<filter> | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | The optional C<filter> param will be used to filter the records if | ||||||||||||||||||||||||||||
| 188 | provided. It should be a C<CODE> reference or any otherwise callable | ||||||||||||||||||||||||||||
| 189 | scalar, and each value parsed (either array reference or hash reference) | ||||||||||||||||||||||||||||
| 190 | will be available to the filter as C<$_> to be changed or converted into an object, | ||||||||||||||||||||||||||||
| 191 | or whatever you wish. See the L</Writing Filters> section for more details. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =back | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =cut | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 9 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=20 SCAR=35.5
Pod
documented
| |||||||||||||||||||||||||||||
| 198 | 9 | my $class = shift; | |||||||||||||||||||||||||||
| 199 | 9 | my $self = bless { | |||||||||||||||||||||||||||
| 200 | @_, | ||||||||||||||||||||||||||||
| 201 | row => 0, | ||||||||||||||||||||||||||||
| 202 | errstr => '', | ||||||||||||||||||||||||||||
| 203 | }, $class; | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | # Do we have a file name | ||||||||||||||||||||||||||||
| 206 | 9 | ▶ | if ( exists $self->{file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 7 | ▶ | unless ( Params::Util::_STRING($self->{file}) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 0 | Carp::croak("Parse::CSV file param is not a string"); | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | 7 | ▶ | unless ( -f $self->{file} and -r _ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-f $self->{"file"} and -r _
MC/DC0%0/2
-f $self->{"file"} and -r _
-f $self->{"file"}
-r _
Truth table33%1/3
A and B
A= -f $self->{"file"}B= -r _
| |||||||||||||||||||||||||||||
| 211 | 0 | Carp::croak("Parse::CSV file '$self->{file}' does not exist"); | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 7 | $self->{handle} = IO::File->new(); | |||||||||||||||||||||||||||
| 215 | 7 | ▶ | unless ( $self->{handle}->open($self->{file}) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 0 | Carp::croak("Parse::CSV file '$self->{file}' failed to load: $!"); | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | # Do we have a file handle | ||||||||||||||||||||||||||||
| 221 | 9 | ▶ | if ( exists $self->{handle} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 9 | ▶ | unless ( Params::Util::_HANDLE($self->{handle}) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 0 | Carp::croak("Parse::CSV handle param is not an IO handle"); | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | } else { | ||||||||||||||||||||||||||||
| 226 | 0 | Carp::croak("Parse::CSV not provided a file or handle param"); | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # Separate the Text::CSV attributes | ||||||||||||||||||||||||||||
| 230 | 9 | ▶ | unless ( Params::Util::_HASH0($self->{csv_attr}) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 7 | $self->{csv_attr} = {binary => 1}; # Suggested by Text::CSV_XS docs to always be on | |||||||||||||||||||||||||||
| 232 | # XXX it would be nice to not have this list hard-coded. | ||||||||||||||||||||||||||||
| 233 | 7 | foreach ( qw{quote_char eol escape_char sep_char binary always_quote} ) { | |||||||||||||||||||||||||||
| 234 | 42 | ▶ | next unless exists $self->{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 0 | $self->{csv_attr}->{$_} = delete $self->{$_}; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | # Create the parser | ||||||||||||||||||||||||||||
| 240 | 9 | $self->{csv_xs} = Text::CSV_XS->new( $self->{csv_attr} ); | |||||||||||||||||||||||||||
| 241 | 9 | ▶ | unless ( $self->{csv_xs} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | 0 | Carp::croak("Failed to create Text::CSV_XS parser"); | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | # Deprecated fields usage | ||||||||||||||||||||||||||||
| 246 | 9 | ▶ | if ( $self->{fields} and not $self->{names} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"fields"} and not $self->{"names"}
MC/DC50%1/2
$self->{"fields"} and not $self->{"names"}
$self->{"fields"}
not $self->{"names"}
Truth table66%2/3
A and not(B)
A= $self->{"fields"}B= not $self->{"names"}
| |||||||||||||||||||||||||||||
| 247 | 3 | $self->{names} = $self->{fields}; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | # Handle automatic field names | ||||||||||||||||||||||||||||
| 251 | 9 | ▶ | if ( Params::Util::_STRING($self->{names}) and $self->{names} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
&Params::Util::_STRING($self->{"names"}) and $self->{"names"}
MC/DC50%1/2
&Params::Util::_STRING($self->{"names"}) and $self->{"names"}
&Params::Util::_STRING($self->{"names"})
$self->{"names"}
Truth table66%2/3
A and B
A= &Params::Util::_STRING($self->{"names"})B= $self->{"names"}
| |||||||||||||||||||||||||||||
| 252 | # Grab the first line | ||||||||||||||||||||||||||||
| 253 | 5 | $self->{names} = $self->getline(header=>1); | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | # Check names | ||||||||||||||||||||||||||||
| 257 | 9 | ▶ | if ( exists $self->{names} and ! Params::Util::_ARRAY($self->{names}) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $self->{"names"} and not &Params::Util::_ARRAY($self->{"names"})
MC/DC0%0/2
exists $self->{"names"} and not &Params::Util::_ARRAY($self->{"names"})
exists $self->{"names"}
not &Params::Util::_ARRAY($self->{"names"})
Truth table66%2/3
A and not(B)
A= exists $self->{"names"}B= not &Params::Util::_ARRAY($self->{"names"})
| |||||||||||||||||||||||||||||
| 258 | 0 | Carp::croak("Parse::CSV names param is not an array reference of strings"); | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | # Check filter | ||||||||||||||||||||||||||||
| 262 | 9 | ▶ | if ( exists $self->{filter} and ! Params::Util::_CODELIKE($self->{filter}) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $self->{"filter"} and not &Params::Util::_CODELIKE($self->{"filter"})
MC/DC0%0/2
exists $self->{"filter"} and not &Params::Util::_CODELIKE($self->{"filter"})
exists $self->{"filter"}
not &Params::Util::_CODELIKE($self->{"filter"})
Truth table66%2/3
A and not(B)
A= exists $self->{"filter"}B= not &Params::Util::_CODELIKE($self->{"filter"})
| |||||||||||||||||||||||||||||
| 263 | 0 | Carp::croak("Parse::CSV filter param is not callable"); | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 9 | $self; | |||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | ##################################################################### | ||||||||||||||||||||||||||||
| 274 | # Main Methods | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =pod | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | =head2 fetch | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | Once a L<Parse::CSV> object has been created, the C<fetch> method is | ||||||||||||||||||||||||||||
| 281 | used to parse and return the next value from the CSV file. | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | Returns an C<ARRAY>, C<HASH> or the output of the filter, based on the | ||||||||||||||||||||||||||||
| 284 | configuration of the object, or C<undef> in a variety of situations. | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | Returning C<undef> means either some part of the parsing and filtering | ||||||||||||||||||||||||||||
| 287 | process has resulted in an error, B<or> that the end of file has been | ||||||||||||||||||||||||||||
| 288 | reached. | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | On receiving C<undef>, you should check the C<errstr> method. If it is an empty | ||||||||||||||||||||||||||||
| 291 | string you have reached the end of file. Otherwise the error message will | ||||||||||||||||||||||||||||
| 292 | be returned. Thus, the basic usage of L<Parse::CSV> will look like the | ||||||||||||||||||||||||||||
| 293 | following. | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | my $parser = Parse::CSV->new( | ||||||||||||||||||||||||||||
| 296 | file => 'file.csv', | ||||||||||||||||||||||||||||
| 297 | ); | ||||||||||||||||||||||||||||
| 298 | while ( my $value = $parser->fetch ) { | ||||||||||||||||||||||||||||
| 299 | # Do something... | ||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | if ( $parser->errstr ) { | ||||||||||||||||||||||||||||
| 302 | # Handle errors... | ||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | NOTE: currently the L</fields> and L</string> methods can be used to | ||||||||||||||||||||||||||||
| 306 | access the most recently-read row (as an array ref or a formatted | ||||||||||||||||||||||||||||
| 307 | string) after using C</fetch>. However, this contradicts the | ||||||||||||||||||||||||||||
| 308 | documentation for L<Text::CSV_XS>, which says those methods should be | ||||||||||||||||||||||||||||
| 309 | "meaningless" after calling C<getline> (which C<fetch()> internally | ||||||||||||||||||||||||||||
| 310 | uses to read the input). Keeping the current behavior also incurs a | ||||||||||||||||||||||||||||
| 311 | speed & memory penalty. Therefore, relying on L</fields> and L</string> | ||||||||||||||||||||||||||||
| 312 | to return the current data after C<fetch()> is deprecated and will | ||||||||||||||||||||||||||||
| 313 | (probably) be removed in a future release. | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | =cut | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 24 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 318 | 24 | my $self = shift; | |||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | # The filter can skip rows, | ||||||||||||||||||||||||||||
| 321 | # iterate till we get something. | ||||||||||||||||||||||||||||
| 322 | 24 | ▶ | while ( my $row = $self->getline ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | # Turn the array ref into a hash if needed | ||||||||||||||||||||||||||||
| 324 | 19 | my $rv; | |||||||||||||||||||||||||||
| 325 | 19 | ▶ | if ( $self->{names} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | 9 | $rv = {}; | |||||||||||||||||||||||||||
| 327 | 9 | @{$rv}{@{$self->{names}}} = @$row; | |||||||||||||||||||||||||||
| 328 | } else { | ||||||||||||||||||||||||||||
| 329 | 10 | $rv = $row; | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | # Just return for simple uses | ||||||||||||||||||||||||||||
| 333 | 19 | ▶ | return $rv unless $self->{filter}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | # Filter if needed | ||||||||||||||||||||||||||||
| 336 | 4 | $rv = eval { local $_ = $rv; $self->{filter}->() }; | |||||||||||||||||||||||||||
| 337 | 4 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 338 | # Handle filter errors | ||||||||||||||||||||||||||||
| 339 | 0 | $self->{errstr} = "Filter error: $@"; | |||||||||||||||||||||||||||
| 340 | 0 | $self->{errstr} =~ s/^(.+)at line.+$/$1/; | |||||||||||||||||||||||||||
| 341 | 0 | return undef; | |||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | # Filter returns undef to drop a record | ||||||||||||||||||||||||||||
| 345 | 4 | ▶ | next unless defined $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | # We have a good record, return it | ||||||||||||||||||||||||||||
| 348 | 3 | return $rv; | |||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 6 | return undef; | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | =head2 getline | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | Returns the next line of the input as an array reference, without | ||||||||||||||||||||||||||||
| 357 | performing possible conversion to a hash, and without running any | ||||||||||||||||||||||||||||
| 358 | filters. This is the routine that C<fetch()> uses internally to read | ||||||||||||||||||||||||||||
| 359 | its input. It may be useful if you sometimes want to do filtering and | ||||||||||||||||||||||||||||
| 360 | sometimes don't, or sometimes want to do hash conversion and sometimes | ||||||||||||||||||||||||||||
| 361 | don't, or maybe you don't need either of those things and you just | ||||||||||||||||||||||||||||
| 362 | want to shave all the milliseconds off that you can (but then you | ||||||||||||||||||||||||||||
| 363 | might be better off just using C<Text::CSV> directly). | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =cut | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 30 | ▶ | sub getline { | ||||||||||||||||||||||||||
|
Subroutine
getline: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 368 | 30 | my $self = shift; | |||||||||||||||||||||||||||
| 369 | 30 | my %attrs = @_; | |||||||||||||||||||||||||||
| 370 | 30 | $self->{errstr} = ''; | |||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | my $row = $attrs{header} | ||||||||||||||||||||||||||||
| 373 | ? [$self->{csv_xs}->header( $self->{handle} )] | ||||||||||||||||||||||||||||
| 374 | 30 | ▶ | : $self->{csv_xs}->getline( $self->{handle} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 30 | ▶ | if (!$row && 0+$self->{csv_xs}->error_diag) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $row and 0 + $self->{"csv_xs"}->error_diag
MC/DC50%1/2
not $row and 0 + $self->{"csv_xs"}->error_diag
not $row
0 + $self->{"csv_xs"}->error_diag
Truth table66%2/3
not(A) and B
A= not $rowB= 0 + $self->{"csv_xs"}->error_diag
| |||||||||||||||||||||||||||||
| 377 | 6 | my $err = "".$self->{csv_xs}->error_diag; | |||||||||||||||||||||||||||
| 378 | # We need to propagate errors from Text::CSV_XS, but | ||||||||||||||||||||||||||||
| 379 | # eof is also reported as an error. So we are going to | ||||||||||||||||||||||||||||
| 380 | # filter out it as a special case. | ||||||||||||||||||||||||||||
| 381 | 6 | ▶ | if (!eof $self->{handle} || $err !~ /^EOF/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
eof $self->{"handle"} and $err =~ /^EOF/
MC/DC50%1/2
eof $self->{"handle"} and $err =~ /^EOF/
eof $self->{"handle"}
$err =~ /^EOF/
Truth table66%2/3
A and B
A= eof $self->{"handle"}B= $err =~ /^EOF/
| |||||||||||||||||||||||||||||
| 382 | 1 | $self->{errstr} = $err; | |||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | 30 | ▶ | $self->{row}++ if defined $row; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 387 | 30 | $self->{savedrow} = $row; | |||||||||||||||||||||||||||
| 388 | 30 | return $row; | |||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | =pod | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | =head2 row | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | The C<row> method returns the line number of the most-recently-read row of the CSV file. | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | This is a one-based count, so when you first create the parser, | ||||||||||||||||||||||||||||
| 398 | the value of C<row> will be zero (unless you are using | ||||||||||||||||||||||||||||
| 399 | C<names> on automatic in which case it will be 1). | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =cut | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | 23 | ▶ | sub row { | ||||||||||||||||||||||||||
|
Subroutine
row: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 404 | 23 | $_[0]->{row}; | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | =pod | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | =head2 combine | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | $status = $csv->combine(@columns); | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | The C<combine> method is provided as a convenience, and is passed through | ||||||||||||||||||||||||||||
| 414 | to the underlying L<Text::CSV_XS> object. | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =cut | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 4 | ▶ | sub combine { | ||||||||||||||||||||||||||
|
Subroutine
combine: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 419 | 4 | shift->{csv_xs}->combine(@_); | |||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | =pod | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | =head2 string | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | $line = $csv->string; | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | The C<string> method is provided as a convenience, and is passed through | ||||||||||||||||||||||||||||
| 429 | to the underlying L<Text::CSV_XS> object. | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | NOTE: relying on L</string> to return the current data after C<fetch()> | ||||||||||||||||||||||||||||
| 432 | is deprecated and will (probably) be removed in a future release. | ||||||||||||||||||||||||||||
| 433 | Only rely on its value after C<combine()>. See similar warnings in | ||||||||||||||||||||||||||||
| 434 | L</fetch> and L</fields>. | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | =cut | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | 2 | ▶ | sub string { | ||||||||||||||||||||||||||
|
Subroutine
string: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 439 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 440 | 2 | ▶ | if ($self->{savedrow}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 441 | 2 | $self->combine(@{$self->{savedrow}}); | |||||||||||||||||||||||||||
| 442 | 2 | delete $self->{savedrow}; | |||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | 2 | $self->{csv_xs}->string; | |||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | =pod | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | =head2 print | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | $status = $csv->print($io, $columns); | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | The C<print> method is provided as a convenience, and is passed through | ||||||||||||||||||||||||||||
| 454 | to the underlying L<Text::CSV_XS> object. | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | =cut | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | 0 | ▶ | sub print { | ||||||||||||||||||||||||||
|
Subroutine
print: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 459 | 0 | shift->{csv_xs}->print(@_); | |||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | =pod | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | =head2 fields | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | @fields = $csv->fields; | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | The C<fields> method is provided as a convenience, and is passed through | ||||||||||||||||||||||||||||
| 469 | to the underlying L<Text::CSV_XS> object. It shows the actual row as an array. | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | NOTE: relying on L<fields> to return the current data after C<fetch()> | ||||||||||||||||||||||||||||
| 472 | is deprecated and will (probably) be removed in a future release. | ||||||||||||||||||||||||||||
| 473 | Only rely on its value after C<combine()>. See similar warnings in | ||||||||||||||||||||||||||||
| 474 | L</fetch> and L</string>. | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | =cut | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 4 | ▶ | sub fields { | ||||||||||||||||||||||||||
|
Subroutine
fields: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 479 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 480 | 4 | ▶ | if ($self->{savedrow}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 481 | 2 | $self->combine(@{$self->{savedrow}}); | |||||||||||||||||||||||||||
| 482 | 2 | delete $self->{savedrow}; | |||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||
| 484 | 4 | $self->{csv_xs}->fields; | |||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | =pod | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | =head2 names | ||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | # Get the current column names in use | ||||||||||||||||||||||||||||
| 492 | my @names = $csv->names; | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | # Change the column names on the fly mid stream | ||||||||||||||||||||||||||||
| 495 | $csv->names( 'fn1', 'fn2' ); | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | The C<names> method gets or sets the column name mapping for the parser. | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | If the parser has no names or fields, returns the null list. | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | =cut | ||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | 6 | ▶ | sub names { | ||||||||||||||||||||||||||
|
Subroutine
names: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 504 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 505 | 6 | my $names = $self->{names}; | |||||||||||||||||||||||||||
| 506 | 6 | ▶ | if ( $names ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 507 | 5 | ▶ | @$names = @_ if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 508 | 5 | return @$names; | |||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||
| 510 | 1 | ▶ | $self->{names} = [ @_ ] if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 511 | 1 | return @_; | |||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | =pod | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | =head2 errstr | ||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | On error, the C<errstr> method returns the error that occured. | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | If the last action was NOT an error, returns the null string C<''>. | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | =cut | ||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | 28 | ▶ | sub errstr { | ||||||||||||||||||||||||||
|
Subroutine
errstr: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 525 | 28 | $_[0]->{errstr}; | |||||||||||||||||||||||||||
| 526 | } | ||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | 3 | 1; | |||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | =pod | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | Bugs should always be reported via the CPAN bug tracker at | ||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||
| 536 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Parse-CSV> | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | For other issues, or commercial enhancement or support, contact the author. | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | =head1 CONTRIBUTORS | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | Uwe Sarnowski E<lt>uwes@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | Ken Williams E<lt>kwilliams@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | L<Text::CSV_XS>, L<http://ali.as/> | ||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||
| 556 | Copyright 2006 - 2012 Adam Kennedy. | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||
| 559 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||
| 561 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 562 | LICENSE file included with this module. | ||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | =cut | ||||||||||||||||||||||||||||