.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Text::RecordParser 3pm" .TH Text::RecordParser 3pm "2015-04-21" "perl v5.20.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Text::RecordParser \- read record\-oriented files .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Text::RecordParser; \& \& # use default record (\en) and field (,) separators \& my $p = Text::RecordParser\->new( $file ); \& \& # or be explicit \& my $p = Text::RecordParser\->new({ \& filename => $file, \& field_separator => "\et", \& }); \& \& $p\->filename(\*(Aqfoo.csv\*(Aq); \& \& # Split records on two newlines \& $p\->record_separator("\en\en"); \& \& # Split fields on tabs \& $p\->field_separator("\et"); \& \& # Skip lines beginning with hashes \& $p\->comment( qr/^#/ ); \& \& # Trim whitespace \& $p\->trim(1); \& \& # Use the fields in the first line as column names \& $p\->bind_header; \& \& # Get a list of the header fields (in order) \& my @columns = $p\->field_list; \& \& # Extract a particular field from the next row \& my ( $name, $age ) = $p\->extract( qw[name age] ); \& \& # Return all the fields from the next row \& my @fields = $p\->fetchrow_array; \& \& # Define a field alias \& $p\->set_field_alias( name => \*(Aqhandle\*(Aq ); \& \& # Return all the fields from the next row as a hashref \& my $record = $p\->fetchrow_hashref; \& print $record\->{\*(Aqname\*(Aq}; \& # or \& print $record\->{\*(Aqhandle\*(Aq}; \& \& # Return the record as an object with fields as accessors \& my $object = $p\->fetchrow_object; \& print $object\->name; # or $object\->handle; \& \& # Get all data as arrayref of arrayrefs \& my $data = $p\->fetchall_arrayref; \& \& # Get all data as arrayref of hashrefs \& my $data = $p\->fetchall_arrayref( { Columns => {} } ); \& \& # Get all data as hashref of hashrefs \& my $data = $p\->fetchall_hashref(\*(Aqname\*(Aq); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module is for reading record-oriented data in a delimited text file. The most common example have records separated by newlines and fields separated by commas or tabs, but this module aims to provide a consistent interface for handling sequential records in a file however they may be delimited. Typically this data lists the fields in the first line of the file, in which case you should call \f(CW\*(C`bind_header\*(C'\fR to bind the field name (or not, and it will be called implicitly). If the first line contains data, you can still bind your own field names via \f(CW\*(C`bind_fields\*(C'\fR. Either way, you can then use many methods to get at the data as arrays or hashes. .SH "METHODS" .IX Header "METHODS" .SS "new" .IX Subsection "new" This is the object constructor. It takes a hash (or hashref) of arguments. Each argument can also be set through the method of the same name. .IP "\(bu" 4 filename .Sp The path to the file being read. If the filename is passed and the fh is not, then it will open a filehandle on that file and sets \f(CW\*(C`fh\*(C'\fR accordingly. .IP "\(bu" 4 comment .Sp A compiled regular expression identifying comment lines that should be skipped. .IP "\(bu" 4 data .Sp The data to read. .IP "\(bu" 4 fh .Sp The filehandle of the file to read. .IP "\(bu" 4 field_separator | fs .Sp The field separator (default is comma). .IP "\(bu" 4 record_separator | rs .Sp The record separator (default is newline). .IP "\(bu" 4 field_filter .Sp A callback applied to all the fields as they are read. .IP "\(bu" 4 header_filter .Sp A callback applied to the column names. .IP "\(bu" 4 trim .Sp Boolean to enable trimming of leading and trailing whitespace from fields (useful if splitting on whitespace only). .PP See methods for each argument name for more information. .PP Alternately, if you supply a single argument to \f(CW\*(C`new\*(C'\fR, it will be treated as the \f(CW\*(C`filename\*(C'\fR argument. .SS "bind_fields" .IX Subsection "bind_fields" .Vb 1 \& $p\->bind_fields( qw[ name rank serial_number ] ); .Ve .PP Takes an array of field names and memorizes the field positions for later use. If the input file has no header line but you still wish to retrieve the fields by name (or even if you want to call \&\f(CW\*(C`bind_header\*(C'\fR and then give your own field names), simply pass in the an array of field names you wish to use. .PP Pass in an empty array reference to unset: .PP .Vb 1 \& $p\->bind_field( [] ); # unsets fields .Ve .SS "bind_header" .IX Subsection "bind_header" .Vb 2 \& $p\->bind_header; \& my $name = $p\->extract(\*(Aqname\*(Aq); .Ve .PP Takes the fields from the next row under the cursor and assigns the field names to the values. Usually you would call this immediately after opening the file in order to bind the field names in the first row. .SS "comment" .IX Subsection "comment" .Vb 2 \& $p\->comment( qr/^#/ ); # Perl\-style comments \& $p\->comment( qr/^\-\-/ ); # SQL\-style comments .Ve .PP Takes a regex to apply to a record to see if it looks like a comment to skip. .SS "data" .IX Subsection "data" .Vb 5 \& $p\->data( $string ); \& $p\->data( \e$string ); \& $p\->data( @lines ); \& $p\->data( [$line1, $line2, $line3] ); \& $p\->data( IO::File\->new(\*(Aqextract( qw[ foo bar baz ] ); .Ve .PP Extracts a list of fields out of the last row read. The field names must correspond to the field names bound either via \f(CW\*(C`bind_fields\*(C'\fR or \&\f(CW\*(C`bind_header\*(C'\fR. .SS "fetchrow_array" .IX Subsection "fetchrow_array" .Vb 1 \& my @values = $p\->fetchrow_array; .Ve .PP Reads a row from the file and returns an array or array reference of the fields. .SS "fetchrow_hashref" .IX Subsection "fetchrow_hashref" .Vb 2 \& my $record = $p\->fetchrow_hashref; \& print "Name = ", $record\->{\*(Aqname\*(Aq}, "\en"; .Ve .PP Reads a line of the file and returns it as a hash reference. The keys of the hashref are the field names bound via \f(CW\*(C`bind_fields\*(C'\fR or \&\f(CW\*(C`bind_header\*(C'\fR. If you do not bind fields prior to calling this method, the \f(CW\*(C`bind_header\*(C'\fR method will be implicitly called for you. .SS "fetchrow_object" .IX Subsection "fetchrow_object" .Vb 4 \& while ( my $object = $p\->fetchrow_object ) { \& my $id = $object\->id; \& my $name = $object\->naem; # <\-\- this will throw a runtime error \& } .Ve .PP This will return the next data record as a Text::RecordParser::Object object that has read-only accessor methods of the field names and any aliases. This allows you to enforce field names, further helping ensure that your code is reading the input file correctly. That is, if you are using the \*(L"fetchrow_hashref\*(R" method to read each line, you may misspell the hash key and introduce a bug in your code. With this method, Perl will throw an error if you attempt to read a field not defined in the file's headers. Additionally, any defined field aliases will be created as additional accessor methods. .SS "fetchall_arrayref" .IX Subsection "fetchall_arrayref" .Vb 4 \& my $records = $p\->fetchall_arrayref; \& for my $record ( @$records ) { \& print "Name = ", $record\->[0], "\en"; \& } \& \& my $records = $p\->fetchall_arrayref( { Columns => {} } ); \& for my $record ( @$records ) { \& print "Name = ", $record\->{\*(Aqname\*(Aq}, "\en"; \& } .Ve .PP Like \s-1DBI\s0's fetchall_arrayref, returns an arrayref of arrayrefs. Also accepts optional \*(L"{ Columns => {} }\*(R" argument to return an arrayref of hashrefs. .SS "fetchall_hashref" .IX Subsection "fetchall_hashref" .Vb 5 \& my $records = $p\->fetchall_hashref(\*(Aqid\*(Aq); \& for my $id ( keys %$records ) { \& my $record = $records\->{ $id }; \& print "Name = ", $record\->{\*(Aqname\*(Aq}, "\en"; \& } .Ve .PP Like \s-1DBI\s0's fetchall_hashref, this returns a hash reference of hash references. The keys of the top-level hashref are the field values of the field argument you supply. The field name you supply can be a field created by a \f(CW\*(C`field_compute\*(C'\fR. .SS "fh" .IX Subsection "fh" .Vb 2 \& open my $fh, \*(Aq<\*(Aq, $file or die $!; \& $p\->fh( $fh ); .Ve .PP Gets or sets the filehandle of the file being read. .SS "field_compute" .IX Subsection "field_compute" A callback applied to the fields identified by position (or field name if \f(CW\*(C`bind_fields\*(C'\fR or \f(CW\*(C`bind_header\*(C'\fR was called). .PP The callback will be passed two arguments: .IP "1." 4 The current field .IP "2." 4 A reference to all the other fields, either as an array or hash reference, depending on the method which you called. .PP If data looks like this: .PP .Vb 3 \& parent children \& Mike Greg,Peter,Bobby \& Carol Marcia,Jane,Cindy .Ve .PP You could split the \*(L"children\*(R" field into an array reference with the values like so: .PP .Vb 1 \& $p\->field_compute( \*(Aqchildren\*(Aq, sub { [ split /,/, shift() ] } ); .Ve .PP The field position or name doesn't actually have to exist, which means you could create new, computed fields on-the-fly. E.g., if you data looks like this: .PP .Vb 3 \& 1,3,5 \& 32,4,1 \& 9,5,4 .Ve .PP You could write a field_compute like this: .PP .Vb 8 \& $p\->field_compute( 3, \& sub { \& my ( $cur, $others ) = @_; \& my $sum; \& $sum += $_ for @$others; \& return $sum; \& } \& ); .Ve .PP Field \*(L"3\*(R" will be created as the sum of the other fields. This allows you to further write: .PP .Vb 4 \& my $data = $p\->fetchall_arrayref; \& for my $rec ( @$data ) { \& print "$rec\->[0] + $rec\->[1] + $rec\->[2] = $rec\->[3]\en"; \& } .Ve .PP Prints: .PP .Vb 3 \& 1 + 3 + 5 = 9 \& 32 + 4 + 1 = 37 \& 9 + 5 + 4 = 18 .Ve .SS "field_filter" .IX Subsection "field_filter" .Vb 1 \& $p\->field_filter( sub { $_ = shift; uc(lc($_)) } ); .Ve .PP A callback which is applied to each field. The callback will be passed the current value of the field. Whatever is passed back will become the new value of the field. The above example capitalizes field values. To unset the filter, pass in the empty string. .SS "field_list" .IX Subsection "field_list" .Vb 3 \& $p\->bind_fields( qw[ foo bar baz ] ); \& my @fields = $p\->field_list; \& print join \*(Aq, \*(Aq, @fields; # prints "foo, bar, baz" .Ve .PP Returns the fields bound via \f(CW\*(C`bind_fields\*(C'\fR (or \f(CW\*(C`bind_header\*(C'\fR). .SS "field_positions" .IX Subsection "field_positions" .Vb 1 \& my %positions = $p\->field_positions; .Ve .PP Returns a hash of the fields and their positions bound via \&\f(CW\*(C`bind_fields\*(C'\fR (or \f(CW\*(C`bind_header\*(C'\fR). Mostly for internal use. .SS "field_separator" .IX Subsection "field_separator" .Vb 4 \& $p\->field_separator("\et"); # splits fields on tabs \& $p\->field_separator(\*(Aq::\*(Aq); # splits fields on double colons \& $p\->field_separator(qr/\es+/); # splits fields on whitespace \& my $sep = $p\->field_separator; # returns the current separator .Ve .PP Gets and sets the token to use as the field delimiter. Regular expressions can be specified using qr//. If not specified, it will take a guess based on the filename extension (\*(L"comma\*(R" for \*(L".txt,\*(R" \&\*(L".dat,\*(R" or \*(L".csv\*(R"; \*(L"tab\*(R" for \*(L".tab\*(R"). The default is a comma. .SS "filename" .IX Subsection "filename" .Vb 1 \& $p\->filename(\*(Aq/path/to/file.dat\*(Aq); .Ve .PP Gets or sets the complete path to the file to be read. If a file is already opened, then the handle on it will be closed and a new one opened on the new file. .SS "get_field_aliases" .IX Subsection "get_field_aliases" .Vb 1 \& my @aliases = $p\->get_field_aliases(\*(Aqname\*(Aq); .Ve .PP Allows you to define alternate names for fields, e.g., sometimes your input file calls city \*(L"town\*(R" or \*(L"township,\*(R" sometimes a file uses \*(L"Moniker\*(R" instead of \*(L"name.\*(R" .SS "header_filter" .IX Subsection "header_filter" .Vb 1 \& $p\->header_filter( sub { $_ = shift; s/\es+/_/g; lc $_ } ); .Ve .PP A callback applied to column header names. The callback will be passed the current value of the header. Whatever is returned will become the new value of the header. The above example collapses spaces into a single underscore and lowercases the letters. To unset a filter, pass in the empty string. .SS "record_separator" .IX Subsection "record_separator" .Vb 2 \& $p\->record_separator("\en//\en"); \& $p\->field_separator("\en"); .Ve .PP Gets and sets the token to use as the record separator. The default is a newline (\*(L"\en\*(R"). .PP The above example would read a file that looks like this: .PP .Vb 8 \& field1 \& field2 \& field3 \& // \& data1 \& data2 \& data3 \& // .Ve .SS "set_field_alias" .IX Subsection "set_field_alias" .Vb 4 \& $p\->set_field_alias({ \& name => \*(AqMoniker,handle\*(Aq, # comma\-separated string \& city => [ qw( town township ) ], # or anonymous arrayref \& }); .Ve .PP Allows you to define alternate names for fields, e.g., sometimes your input file calls city \*(L"town\*(R" or \*(L"township,\*(R" sometimes a file uses \*(L"Moniker\*(R" instead of \*(L"name.\*(R" .SS "trim" .IX Subsection "trim" .Vb 1 \& my $trim_value = $p\->trim(1); .Ve .PP Provide \*(L"true\*(R" argument to remove leading and trailing whitespace from fields. Use a \*(L"false\*(R" argument to disable. .SH "AUTHOR" .IX Header "AUTHOR" Ken Youens-Clark .SH "SOURCE" .IX Header "SOURCE" http://github.com/kyclark/text\-recordparser .SH "CREDITS" .IX Header "CREDITS" Thanks to the following: .IP "\(bu" 4 Benjamin Tilly .Sp For Text::xSV, the inspirado for this module .IP "\(bu" 4 Tim Bunce et al. .Sp For \s-1DBI,\s0 from which many of the methods were shamelessly stolen .IP "\(bu" 4 Tom Aldcroft .Sp For contributing code to make it easy to parse whitespace-delimited data .IP "\(bu" 4 Liya Ren .Sp For catching the column-ordering error when parsing with \*(L"no-headers\*(R" .IP "\(bu" 4 Sharon Wei .Sp For catching bug in \f(CW\*(C`extract\*(C'\fR that sets up infinite loops .IP "\(bu" 4 Lars Thegler .Sp For bug report on missing \*(L"script_files\*(R" arg in Build.PL .SH "BUGS" .IX Header "BUGS" None known. Please use http://rt.cpan.org/ for reporting bugs. .SH "LICENSE AND COPYRIGHT" .IX Header "LICENSE AND COPYRIGHT" Copyright (C) 2006\-10 Ken Youens-Clark. All rights reserved. .PP This program is free software; you can redistribute it and/or modify it under the terms of the \s-1GNU\s0 General Public License as published by the Free Software Foundation; version 2. .PP This program is distributed in the hope that it will be useful, but \&\s-1WITHOUT ANY WARRANTY\s0; without even the implied warranty of \&\s-1MERCHANTABILITY\s0 or \s-1FITNESS FOR A PARTICULAR PURPOSE. \s0 See the \s-1GNU\s0 General Public License for more details.