James E Keenan > Parse-File-Metadata-0.07 > Parse::File::Metadata

Download:
Parse-File-Metadata-0.07.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.07   Source  

NAME ^

Parse::File::Metadata - For plain-text files that contain both metadata and data records, parse metadata first

SYNOPSIS ^

     use Parse::File::Metadata;

    $metaref = {};
    @rules = (
        {
            rule => sub { exists $metaref->{d}; },
            label => q{'d' key must exist},
        },
        {
            rule => sub { $metaref->{d} =~ /^\d+$/; },
            label => q{'d' key must be non-negative integer},
        },
        {
            rule => sub { exists $metaref->{f}; },
            label => q{'f' key must exist},
        },
    );

    $self = Parse::File::Metadata->new( {
        file            => 'path/to/myfile',
        header_split    => '\s*=\s*',
        metaref         => $metaref,
        rules           => \@rules,
    } );

    $dataprocess = sub { my @fields = split /,/, $_[0], -1; print "@fields\n"; };

    $self->process_metadata_and_proceed( $dataprocess );

    $self->process_metadata_only();

    $metadata_out = $self->get_metadata();

    $exception = $self->get_exception();

DESCRIPTION ^

This module is useful when you have to parse a plain-text file that meets the following conditions:

Example

Below is a plain-text file in which the header consists of key-value pairs delimited by = signs. The key is the to the left of the first delimiter. Everything to the right is part of the value (including any additional delimiter characters).

The body consists of comma-delimited strings. Whether in the body or the header, comments begin with a # sign and are ignored.

    # comment
    a=alpha
    b=beta,charlie,delta
    c=epsilon   zeta    eta
    d=1234567890
    e=This is a string
    f=,
    
    some,body,loves,me
    I,wonder,wonder,who
    could,it,be,you

Suppose you are told that you should proceed to parse the body if and only if the following conditions are met in the header:

This file would meet all three criteria and the program would proceed to parse the three data records.

If, however, metadata element f were commented out:

    #f=,

the file would no longer meet the criteria and the program would cease before parsing the data records.

METHODS ^

new()

process_metadata_and_proceed()

process_metadata_only()

get_metadata()

get_exception()

SUPPORT ^

https://rt.cpan.org

AUTHOR ^

    James E Keenan
    CPAN ID: jkeenan
    Perl Seminar NY
    jkeenan@cpan.org
    http://thenceforward.net/perl/modules/Parse-File-Metadata

COPYRIGHT ^

Copyright 2010 James E Keenan

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.

SEE ALSO ^

perl(1).

syntax highlighting: