NAME
Parse::File::Taxonomy::Path - Validate a file for use as a path-based taxonomy
SYNOPSIS
use Parse::File::Taxonomy::Path;
# 'file' interface: reads a CSV file for you
$source = "./t/data/alpha.csv";
$obj = Parse::File::Taxonomy::Path->new( {
file => $source,
} );
# 'components' interface: as if you've already read a
# CSV file and now have Perl array references to header and data rows
$obj = Parse::File::Taxonomy::Path->new( {
components => {
fields => $fields,
data_records => $data_records,
}
} );
METHODS
new()
Purpose
Parse::File::Taxonomy::Path constructor.
Arguments
Single hash reference. There are two possible interfaces:
file
andcomponents
.- 1
file
interface -
$source = "./t/data/alpha.csv"; $obj = Parse::File::Taxonomy::Path->new( { file => $source, path_col_idx => 0, path_col_sep => '|', %TextCSVoptions, } );
Elements in the hash reference are keyed on:
file
Absolute or relative path to the incoming taxonomy file. Required for this interface.
path_col_idx
If the column to be used as the "path" column in the incoming taxonomy file is not the first column, this option must be set to the integer representing the "path" column's index position (count starts at 0). Optional; defaults to
0
.path_col_sep
If the string used to distinguish components of the path in the path column in the incoming taxonomy file is not a pipe (
|
), this option must be set. Optional; defaults to|
.Text::CSV options
Any other options which could normally be passed to
Text::CSV-
new()> will be passed through to that module's constructor. On the recommendation of the Text::CSV documentation,binary
is always set to a true value.
- 2
components
interface -
$obj = Parse::File::Taxonomy::Path->new( { components => { fields => $fields, data_records => $data_records, } } );
Elements in this hash are keyed on:
components
This element is required for the
components
interface. The value of this element is a hash reference with two keys,fields
anddata_records
.fields
is a reference to an array holding the field or column names for the data set.data_records
is a reference to an array of array references, each of the latter arrayrefs holding one record or row from the data set.path_col_idx
Same as in
file
interface above.path_col_sep
Same as in
file
interface above.
- 1
Return Value
Parse::File::Taxonomy::Path object.
Comment
new()
will throw an exception under any of the following conditions:Argument to
new()
is not a reference.Argument to
new()
is not a hash reference.In the
file
interface, unable to locate the file which is the value of thefile
element.Argument to
path_col_idx
element is not an integer.Argument to
path_col_idx
is greater than the index number of the last element in the header row of the incoming taxonomy file, i.e., thepath_col_idx
is wrong.The same field is found more than once in the header row of the incoming taxonomy file.
Unable to open or close the incoming taxonomy file for reading.
In the column designated as the "path" column, the same value is observed more than once.
A non-parent node's parent node cannot be located in the incoming taxonomy file.
A data row has a number of fields different from the number of fields in the header row.
fields()
Purpose
Identify the names of the columns in the taxonomy.
Arguments
my $fields = $self->fields();
No arguments; the information is already inside the object.
Return Value
Reference to an array holding a list of the columns as they appear in the header row of the incoming taxonomy file.
Comment
Read-only.
# Implemented in lib/Parse/File/Taxonomy.pm
path_col_idx()
Purpose
Identify the index position (count starts at 0) of the column in the incoming taxonomy file which serves as the path column.
Arguments
my $path_col_idx = $self->path_col_idx;
No arguments; the information is already inside the object.
Return Value
Integer in the range from 0 to 1 less than the number of columns in the header row.
Comment
Read-only.
path_col()
Purpose
Identify the name of the column in the incoming taxonomy which serves as the path column.
Arguments
my $path_col = $self->path_col;
No arguments; the information is already inside the object.
Return Value
String.
Comment
Read-only.
path_col_sep()
Purpose
Identify the string used to separate path components once the taxonomy has been created. This is just a "getter" and is logically distinct from the option to
new()
which is, in effect, a "setter."Arguments
my $path_col_sep = $self->path_col_sep;
No arguments; the information is already inside the object.
Return Value
String.
Comment
Read-only.
data_records()
Purpose
Once the taxonomy has been validated, get a list of its data rows as a Perl data structure.
Arguments
$data_records = $self->data_records;
None.
Return Value
Reference to array of array references. The array will hold the data records found in the incoming taxonomy file in their order in that file.
Comment
Does not contain any information about the fields in the taxonomy, so you should probably either (a) use in conjunction with
fields()
method above; or (b) usefields_and_data_records()
.
# Implemented in lib/Parse/File/Taxonomy.pm
fields_and_data_records()
Purpose
Once the taxonomy has been validated, get a list of its header and data rows as a Perl data structure.
Arguments
$data_records = $self->fields_and_data_records;
None.
Return Value
Reference to array of array references. The first element in the array will hold the header row (same as output of
fields()
). The remaining elements will hold the data records found in the incoming taxonomy file in their order in that file.
data_records_path_components()
Purpose
Once the taxonomy has been validated, get a list of its data rows as a Perl data structure. In each element of this list, the path is now represented as an array reference rather than a string.
Arguments
$data_records_path_components = $self->data_records_path_components;
None.
Return Value
Reference to array of array references. The array will hold the data records found in the incoming taxonomy file in their order in that file.
Comment
Does not contain any information about the fields in the taxonomy, so you may wish to use this method either (a) use in conjunction with
fields()
method above; or (b) usefields_and_data_records_path_components()
.
fields_and_data_records_path_components()
Purpose
Once the taxonomy has been validated, get a list of its data rows as a Perl data structure. The first element in this list is an array reference holding the header row. In each data element of this list, the path is now represented as an array reference rather than a string.
Arguments
$fields_and_data_records_path_components = $self->fields_and_data_records_path_components;
None.
Return Value
Reference to array of array references. The array will hold the data records found in the incoming taxonomy file in their order in that file.
get_field_position()
Purpose
Identify the index position of a given field within the header row.
Arguments
$index = $obj->get_field_position('income');
Takes a single string holding the name of one of the fields (column names).
Return Value
Integer representing the index position (counting from
0
) of the field provided as argument. Throws exception if the argument is not actually a field.
child_counts()
Purpose
Display the number of descendant (multi-generational) nodes each node in the taxonomy has.
Arguments
$child_counts = $self->child_counts();
None.
Return Value
Reference to hash in which each element is keyed on the value of the path column in the incoming taxonomy file.
get_child_count()
Purpose
Get the total number of descendant nodes for one specific node in a validated taxonomy.
Arguments
$child_count = $self->get_child_count('|Path|To|Node');
String containing node's path as spelled in the taxonomy.
Return Value
Unsigned integer >= 0. Any node whose child count is
0
is by definition a leaf node.Comment
Will throw an exception if the node does not exist or is misspelled.
hashify_taxonomy()
Purpose
Turn a validated taxonomy into a Perl hash keyed on the column designated as the path column.
Arguments
$hashref = $self->hashify_taxonomy();
Takes an optional hashref holding a list of any of the following elements:
remove_leading_path_col_sep
Boolean, defaulting to
0
. By default,hashify_taxonomy()
will spell the key of the hash exactly as the value of the path column is spelled in the taxonomy -- which in turn is the way it was spelled in the incoming file. That is, a path in the taxonomy spelled|Alpha|Beta|Gamma
will be spelled as a key in exactly the same way.However, since in many cases (including the example above) the root node of the taxonomy will be empty, the user may wish to remove the first instance of
path_col_sep
. The user would do so by settingremove_leading_path_col_sep
to a true value.$hashref = $self->hashify_taxonomy( { remove_leading_path_col_sep => 1, } );
In that case they key would now be spelled:
Alpha|Beta|Gamma
.Note further that if the
root_str
switch is set to a true value, any setting toremove_leading_path_col_sep
will be ignored.key_delim
A string which will be used in composing the key of the hashref returned by this method. The user may select this key if she does not want to use the value found in the incoming CSV file (which by default will be the pipe character (
|
) and which may be overridden with thepath_col_sep
argument tonew()
.$hashref = $self->hashify_taxonomy( { key_delim => q{ - }, } );
In the above variant, a path that in the incoming taxonomy file was represented by
|Alpha|Beta|Gamma
will in$hashref
be represented by- Alpha - Beta - Gamma
.root_str
A string which will be used in composing the key of the hashref returned by this method. The user will set this switch if she wishes to have the root note explicitly represented. Using this switch will automatically cause
remove_leading_path_col_sep
to be ignored.Suppose the user wished to have
All Suppliers
be the text for the root node. Suppose further that the user wanted to use the string-
as the delimiter within the key.$hashref = $self->hashify_taxonomy( { root_str => q{All Suppliers}, key_delim => q{ - }, } );
Then incoming path
|Alpha|Beta|Gamma
would be keyed as:All Suppliers - Alpha - Beta - Gamma
Return Value
Hash reference. The number of elements in this hash should be equal to the number of non-header records in the taxonomy.