Sometimes we need data split into records and a simple split on the input record separator ($/) or some other value fails because the values we're splitting on may allowed in other parts of the data. Perhaps they're quoted. Perhaps they're embedded in other data which should not be split up.

This module allows you to specify what you wish to split the data on, but also speficy an "unless" regular expression. If the text in question matches the "unless" regex, it will not be split there. This allows us to do things like split on newlines unless newlines are embedded in quotes.

The constructor takes a hashref of key/value pairs to set the behavior of data records to be created.

split

This is the value to split the data on. It may be either a regular expression or a string.

Defaults to the current input record separator ($/).

unless

Data will be split into records matching the split value unless they also match this value. No default.

If you do not have an unless value, use of this module is overkill.

token

You will probably never need to set this value.

Internally, this module attempts to find a token which does not match any text found in the data to be split and also does not match the split value. This is necessary because we mask the data we don't want to split using this token. This allows us to split the resulting text.

In the unlikely event that the module cannot find a token which is not in the text, you may set the token value yourself to some string value. Do not set it to a regular expression.

chomp

By default, the split value is discarded (chomped) from each record. Set this to a true value to keep the split value on each record. This differs slightly from how it's done with split and capturing parentheses:

split /(\,)/, '3,4,5';

Ordinarily, this results in the following list:

( 3, ',', 4, ',', 5 )

This module assumes you want those values with the preceding record. By setting chomp to false, you get the following list:

( '3,', '4,' 5 )

limit

The default split behavior is similar to this:

split $split_regex, $data;

Setting limit will cause the behavior to act like this:

split $split_regex, $data, $limit

See perldoc -f split for more information about the behavior of limit.

You may not set both limit and trim in the constructor.

trim

By default, we return all records. This means that due to the nature of split and how we're doing things, we sometimes get a trailing null record. However, setting this value causes the module to behave as if we had done this:

split $split_regex, $data, 0;

When split is called with a zero as the third argument, trailing null values are discarded. See perldoc -f split for more information.

You may not set both limit and trim in the constructor.

Note: This does not trim white space around returned records.

fields

By default, individual records are returned as strings. If you set fields, you pass in a hashref of arguments that are identical to what new would take and resulting records are returned as array references processed by a new Data::Record instance.

Example: a quick CSV parser which assumes that commas and newlines may both be in quotes:

Getter/setter for token value. Token must be a string that does not match the split value and is not found in the text.

You can return the current token value if you have set it in your code. If you rely on this module to create a token (this is the normal behavior), it is not available via this method until records is called.

Setting the token to an undefined value causes Data::Record to try and find a token itself.

If the token matches the split value, this method will croak when you attempt to set the token.

If the token is found in the data, the records method will croak when it is called.