| 1 |
NAME |
| 2 |
Text::RecordParser - read record-oriented files |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use Text::RecordParser; |
| 6 |
|
| 7 |
# use default record (\n) and field (,) separators |
| 8 |
my $p = Text::RecordParser->new( $file ); |
| 9 |
|
| 10 |
# or be explicit |
| 11 |
my $p = Text::RecordParser->new({ |
| 12 |
filename => $file, |
| 13 |
field_separator => "\t", |
| 14 |
}); |
| 15 |
|
| 16 |
$p->filename('foo.csv'); |
| 17 |
|
| 18 |
# Split records on two newlines |
| 19 |
$p->record_separator("\n\n"); |
| 20 |
|
| 21 |
# Split fields on tabs |
| 22 |
$p->field_separator("\t"); |
| 23 |
|
| 24 |
# Skip lines beginning with hashes |
| 25 |
$p->comment( qr/^#/ ); |
| 26 |
|
| 27 |
# Trim whitespace |
| 28 |
$p->trim(1); |
| 29 |
|
| 30 |
# Use the fields in the first line as column names |
| 31 |
$p->bind_header; |
| 32 |
|
| 33 |
# Get a list of the header fields (in order) |
| 34 |
my @columns = $p->field_list; |
| 35 |
|
| 36 |
# Extract a particular field from the next row |
| 37 |
my ( $name, $age ) = $p->extract( qw[name age] ); |
| 38 |
|
| 39 |
# Return all the fields from the next row |
| 40 |
my @fields = $p->fetchrow_array; |
| 41 |
|
| 42 |
# Define a field alias |
| 43 |
$p->set_field_alias( name => 'handle' ); |
| 44 |
|
| 45 |
# Return all the fields from the next row as a hashref |
| 46 |
my $record = $p->fetchrow_hashref; |
| 47 |
print $record->{'name'}; |
| 48 |
# or |
| 49 |
print $record->{'handle'}; |
| 50 |
|
| 51 |
# Return the record as an object with fields as accessors |
| 52 |
my $object = $p->fetchrow_object; |
| 53 |
print $object->name; # or $object->handle; |
| 54 |
|
| 55 |
# Get all data as arrayref of arrayrefs |
| 56 |
my $data = $p->fetchall_arrayref; |
| 57 |
|
| 58 |
# Get all data as arrayref of hashrefs |
| 59 |
my $data = $p->fetchall_arrayref( { Columns => {} } ); |
| 60 |
|
| 61 |
# Get all data as hashref of hashrefs |
| 62 |
my $data = $p->fetchall_hashref('name'); |
| 63 |
|
| 64 |
DESCRIPTION |
| 65 |
This module is for reading record-oriented data in a delimited text |
| 66 |
file. The most common example have records separated by newlines and |
| 67 |
fields separated by commas or tabs, but this module aims to provide a |
| 68 |
consistent interface for handling sequential records in a file however |
| 69 |
they may be delimited. Typically this data lists the fields in the first |
| 70 |
line of the file, in which case you should call "bind_header" to bind |
| 71 |
the field name (or not, and it will be called implicitly). If the first |
| 72 |
line contains data, you can still bind your own field names via |
| 73 |
"bind_fields". Either way, you can then use many methods to get at the |
| 74 |
data as arrays or hashes. |
| 75 |
|
| 76 |
METHODS |
| 77 |
new |
| 78 |
This is the object constructor. It takes a hash (or hashref) of |
| 79 |
arguments. Each argument can also be set through the method of the same |
| 80 |
name. |
| 81 |
|
| 82 |
* filename |
| 83 |
|
| 84 |
The path to the file being read. If the filename is passed and the |
| 85 |
fh is not, then it will open a filehandle on that file and sets "fh" |
| 86 |
accordingly. |
| 87 |
|
| 88 |
* comment |
| 89 |
|
| 90 |
A compiled regular expression identifying comment lines that should |
| 91 |
be skipped. |
| 92 |
|
| 93 |
* data |
| 94 |
|
| 95 |
The data to read. |
| 96 |
|
| 97 |
* fh |
| 98 |
|
| 99 |
The filehandle of the file to read. |
| 100 |
|
| 101 |
* field_separator | fs |
| 102 |
|
| 103 |
The field separator (default is comma). |
| 104 |
|
| 105 |
* record_separator | rs |
| 106 |
|
| 107 |
The record separator (default is newline). |
| 108 |
|
| 109 |
* field_filter |
| 110 |
|
| 111 |
A callback applied to all the fields as they are read. |
| 112 |
|
| 113 |
* header_filter |
| 114 |
|
| 115 |
A callback applied to the column names. |
| 116 |
|
| 117 |
* trim |
| 118 |
|
| 119 |
Boolean to enable trimming of leading and trailing whitespace from |
| 120 |
fields (useful if splitting on whitespace only). |
| 121 |
|
| 122 |
See methods for each argument name for more information. |
| 123 |
|
| 124 |
Alternately, if you supply a single argument to "new", it will be |
| 125 |
treated as the "filename" argument. |
| 126 |
|
| 127 |
bind_fields |
| 128 |
$p->bind_fields( qw[ name rank serial_number ] ); |
| 129 |
|
| 130 |
Takes an array of field names and memorizes the field positions for |
| 131 |
later use. If the input file has no header line but you still wish to |
| 132 |
retrieve the fields by name (or even if you want to call "bind_header" |
| 133 |
and then give your own field names), simply pass in the an array of |
| 134 |
field names you wish to use. |
| 135 |
|
| 136 |
Pass in an empty array reference to unset: |
| 137 |
|
| 138 |
$p->bind_field( [] ); # unsets fields |
| 139 |
|
| 140 |
bind_header |
| 141 |
$p->bind_header; |
| 142 |
my $name = $p->extract('name'); |
| 143 |
|
| 144 |
Takes the fields from the next row under the cursor and assigns the |
| 145 |
field names to the values. Usually you would call this immediately after |
| 146 |
opening the file in order to bind the field names in the first row. |
| 147 |
|
| 148 |
comment |
| 149 |
$p->comment( qr/^#/ ); # Perl-style comments |
| 150 |
$p->comment( qr/^--/ ); # SQL-style comments |
| 151 |
|
| 152 |
Takes a regex to apply to a record to see if it looks like a comment to |
| 153 |
skip. |
| 154 |
|
| 155 |
data |
| 156 |
$p->data( $string ); |
| 157 |
$p->data( \$string ); |
| 158 |
$p->data( @lines ); |
| 159 |
$p->data( [$line1, $line2, $line3] ); |
| 160 |
$p->data( IO::File->new('<data') ); |
| 161 |
|
| 162 |
Allows a scalar, scalar reference, glob, array, or array reference as |
| 163 |
the thing to read instead of a file handle. |
| 164 |
|
| 165 |
It's not advised to pass a filehandle to "data" as it will read the |
| 166 |
entire contents of the file rather than one line at a time if you set it |
| 167 |
via "fh". |
| 168 |
|
| 169 |
extract |
| 170 |
my ( $foo, $bar, $baz ) = $p->extract( qw[ foo bar baz ] ); |
| 171 |
|
| 172 |
Extracts a list of fields out of the last row read. The field names must |
| 173 |
correspond to the field names bound either via "bind_fields" or |
| 174 |
"bind_header". |
| 175 |
|
| 176 |
fetchrow_array |
| 177 |
my @values = $p->fetchrow_array; |
| 178 |
|
| 179 |
Reads a row from the file and returns an array or array reference of the |
| 180 |
fields. |
| 181 |
|
| 182 |
fetchrow_hashref |
| 183 |
my $record = $p->fetchrow_hashref; |
| 184 |
print "Name = ", $record->{'name'}, "\n"; |
| 185 |
|
| 186 |
Reads a line of the file and returns it as a hash reference. The keys of |
| 187 |
the hashref are the field names bound via "bind_fields" or |
| 188 |
"bind_header". If you do not bind fields prior to calling this method, |
| 189 |
the "bind_header" method will be implicitly called for you. |
| 190 |
|
| 191 |
fetchrow_object |
| 192 |
while ( my $object = $p->fetchrow_object ) { |
| 193 |
my $id = $object->id; |
| 194 |
my $name = $object->naem; # <-- this will throw a runtime error |
| 195 |
} |
| 196 |
|
| 197 |
This will return the next data record as a Text::RecordParser::Object |
| 198 |
object that has read-only accessor methods of the field names and any |
| 199 |
aliases. This allows you to enforce field names, further helping ensure |
| 200 |
that your code is reading the input file correctly. That is, if you are |
| 201 |
using the "fetchrow_hashref" method to read each line, you may misspell |
| 202 |
the hash key and introduce a bug in your code. With this method, Perl |
| 203 |
will throw an error if you attempt to read a field not defined in the |
| 204 |
file's headers. Additionally, any defined field aliases will be created |
| 205 |
as additional accessor methods. |
| 206 |
|
| 207 |
fetchall_arrayref |
| 208 |
my $records = $p->fetchall_arrayref; |
| 209 |
for my $record ( @$records ) { |
| 210 |
print "Name = ", $record->[0], "\n"; |
| 211 |
} |
| 212 |
|
| 213 |
my $records = $p->fetchall_arrayref( { Columns => {} } ); |
| 214 |
for my $record ( @$records ) { |
| 215 |
print "Name = ", $record->{'name'}, "\n"; |
| 216 |
} |
| 217 |
|
| 218 |
Like DBI's fetchall_arrayref, returns an arrayref of arrayrefs. Also |
| 219 |
accepts optional "{ Columns => {} }" argument to return an arrayref of |
| 220 |
hashrefs. |
| 221 |
|
| 222 |
fetchall_hashref |
| 223 |
my $records = $p->fetchall_hashref('id'); |
| 224 |
for my $id ( keys %$records ) { |
| 225 |
my $record = $records->{ $id }; |
| 226 |
print "Name = ", $record->{'name'}, "\n"; |
| 227 |
} |
| 228 |
|
| 229 |
Like DBI's fetchall_hashref, this returns a hash reference of hash |
| 230 |
references. The keys of the top-level hashref are the field values of |
| 231 |
the field argument you supply. The field name you supply can be a field |
| 232 |
created by a "field_compute". |
| 233 |
|
| 234 |
fh |
| 235 |
open my $fh, '<', $file or die $!; |
| 236 |
$p->fh( $fh ); |
| 237 |
|
| 238 |
Gets or sets the filehandle of the file being read. |
| 239 |
|
| 240 |
field_compute |
| 241 |
A callback applied to the fields identified by position (or field name |
| 242 |
if "bind_fields" or "bind_header" was called). |
| 243 |
|
| 244 |
The callback will be passed two arguments: |
| 245 |
|
| 246 |
1 The current field |
| 247 |
|
| 248 |
2 A reference to all the other fields, either as an array or hash |
| 249 |
reference, depending on the method which you called. |
| 250 |
|
| 251 |
If data looks like this: |
| 252 |
|
| 253 |
parent children |
| 254 |
Mike Greg,Peter,Bobby |
| 255 |
Carol Marcia,Jane,Cindy |
| 256 |
|
| 257 |
You could split the "children" field into an array reference with the |
| 258 |
values like so: |
| 259 |
|
| 260 |
$p->field_compute( 'children', sub { [ split /,/, shift() ] } ); |
| 261 |
|
| 262 |
The field position or name doesn't actually have to exist, which means |
| 263 |
you could create new, computed fields on-the-fly. E.g., if you data |
| 264 |
looks like this: |
| 265 |
|
| 266 |
1,3,5 |
| 267 |
32,4,1 |
| 268 |
9,5,4 |
| 269 |
|
| 270 |
You could write a field_compute like this: |
| 271 |
|
| 272 |
$p->field_compute( 3, |
| 273 |
sub { |
| 274 |
my ( $cur, $others ) = @_; |
| 275 |
my $sum; |
| 276 |
$sum += $_ for @$others; |
| 277 |
return $sum; |
| 278 |
} |
| 279 |
); |
| 280 |
|
| 281 |
Field "3" will be created as the sum of the other fields. This allows |
| 282 |
you to further write: |
| 283 |
|
| 284 |
my $data = $p->fetchall_arrayref; |
| 285 |
for my $rec ( @$data ) { |
| 286 |
print "$rec->[0] + $rec->[1] + $rec->[2] = $rec->[3]\n"; |
| 287 |
} |
| 288 |
|
| 289 |
Prints: |
| 290 |
|
| 291 |
1 + 3 + 5 = 9 |
| 292 |
32 + 4 + 1 = 37 |
| 293 |
9 + 5 + 4 = 18 |
| 294 |
|
| 295 |
field_filter |
| 296 |
$p->field_filter( sub { $_ = shift; uc(lc($_)) } ); |
| 297 |
|
| 298 |
A callback which is applied to each field. The callback will be passed |
| 299 |
the current value of the field. Whatever is passed back will become the |
| 300 |
new value of the field. The above example capitalizes field values. To |
| 301 |
unset the filter, pass in the empty string. |
| 302 |
|
| 303 |
field_list |
| 304 |
$p->bind_fields( qw[ foo bar baz ] ); |
| 305 |
my @fields = $p->field_list; |
| 306 |
print join ', ', @fields; # prints "foo, bar, baz" |
| 307 |
|
| 308 |
Returns the fields bound via "bind_fields" (or "bind_header"). |
| 309 |
|
| 310 |
field_positions |
| 311 |
my %positions = $p->field_positions; |
| 312 |
|
| 313 |
Returns a hash of the fields and their positions bound via "bind_fields" |
| 314 |
(or "bind_header"). Mostly for internal use. |
| 315 |
|
| 316 |
field_separator |
| 317 |
$p->field_separator("\t"); # splits fields on tabs |
| 318 |
$p->field_separator('::'); # splits fields on double colons |
| 319 |
$p->field_separator(qr/\s+/); # splits fields on whitespace |
| 320 |
my $sep = $p->field_separator; # returns the current separator |
| 321 |
|
| 322 |
Gets and sets the token to use as the field delimiter. Regular |
| 323 |
expressions can be specified using qr//. If not specified, it will take |
| 324 |
a guess based on the filename extension ("comma" for ".txt," ".dat," or |
| 325 |
".csv"; "tab" for ".tab"). The default is a comma. |
| 326 |
|
| 327 |
filename |
| 328 |
$p->filename('/path/to/file.dat'); |
| 329 |
|
| 330 |
Gets or sets the complete path to the file to be read. If a file is |
| 331 |
already opened, then the handle on it will be closed and a new one |
| 332 |
opened on the new file. |
| 333 |
|
| 334 |
get_field_aliases |
| 335 |
my @aliases = $p->get_field_aliases('name'); |
| 336 |
|
| 337 |
Allows you to define alternate names for fields, e.g., sometimes your |
| 338 |
input file calls city "town" or "township," sometimes a file uses |
| 339 |
"Moniker" instead of "name." |
| 340 |
|
| 341 |
header_filter |
| 342 |
$p->header_filter( sub { $_ = shift; s/\s+/_/g; lc $_ } ); |
| 343 |
|
| 344 |
A callback applied to column header names. The callback will be passed |
| 345 |
the current value of the header. Whatever is returned will become the |
| 346 |
new value of the header. The above example collapses spaces into a |
| 347 |
single underscore and lowercases the letters. To unset a filter, pass in |
| 348 |
the empty string. |
| 349 |
|
| 350 |
record_separator |
| 351 |
$p->record_separator("\n//\n"); |
| 352 |
$p->field_separator("\n"); |
| 353 |
|
| 354 |
Gets and sets the token to use as the record separator. The default is a |
| 355 |
newline ("\n"). |
| 356 |
|
| 357 |
The above example would read a file that looks like this: |
| 358 |
|
| 359 |
field1 |
| 360 |
field2 |
| 361 |
field3 |
| 362 |
// |
| 363 |
data1 |
| 364 |
data2 |
| 365 |
data3 |
| 366 |
// |
| 367 |
|
| 368 |
set_field_alias |
| 369 |
$p->set_field_alias({ |
| 370 |
name => 'Moniker,handle', # comma-separated string |
| 371 |
city => [ qw( town township ) ], # or anonymous arrayref |
| 372 |
}); |
| 373 |
|
| 374 |
Allows you to define alternate names for fields, e.g., sometimes your |
| 375 |
input file calls city "town" or "township," sometimes a file uses |
| 376 |
"Moniker" instead of "name." |
| 377 |
|
| 378 |
trim |
| 379 |
my $trim_value = $p->trim(1); |
| 380 |
|
| 381 |
Provide "true" argument to remove leading and trailing whitespace from |
| 382 |
fields. Use a "false" argument to disable. |
| 383 |
|
| 384 |
AUTHOR |
| 385 |
Ken Youens-Clark <[email protected]> |
| 386 |
|
| 387 |
SOURCE |
| 388 |
http://github.com/kyclark/text-recordparser |
| 389 |
|
| 390 |
CREDITS |
| 391 |
Thanks to the following: |
| 392 |
|
| 393 |
* Benjamin Tilly |
| 394 |
|
| 395 |
For Text::xSV, the inspirado for this module |
| 396 |
|
| 397 |
* Tim Bunce et al. |
| 398 |
|
| 399 |
For DBI, from which many of the methods were shamelessly stolen |
| 400 |
|
| 401 |
* Tom Aldcroft |
| 402 |
|
| 403 |
For contributing code to make it easy to parse whitespace-delimited |
| 404 |
data |
| 405 |
|
| 406 |
* Liya Ren |
| 407 |
|
| 408 |
For catching the column-ordering error when parsing with |
| 409 |
"no-headers" |
| 410 |
|
| 411 |
* Sharon Wei |
| 412 |
|
| 413 |
For catching bug in "extract" that sets up infinite loops |
| 414 |
|
| 415 |
* Lars Thegler |
| 416 |
|
| 417 |
For bug report on missing "script_files" arg in Build.PL |
| 418 |
|
| 419 |
BUGS |
| 420 |
None known. Please use http://rt.cpan.org/ for reporting bugs. |
| 421 |
|
| 422 |
LICENSE AND COPYRIGHT |
| 423 |
Copyright (C) 2006-10 Ken Youens-Clark. All rights reserved. |
| 424 |
|
| 425 |
This program is free software; you can redistribute it and/or modify it |
| 426 |
under the terms of the GNU General Public License as published by the |
| 427 |
Free Software Foundation; version 2. |
| 428 |
|
| 429 |
This program is distributed in the hope that it will be useful, but |
| 430 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 431 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 432 |
Public License for more details. |
| 433 |
|