The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

File::Parser::Role - Read and prepare parsing of file (or glob) data from some source

VERSION

This document describes File::Parser::Role version 0.2.4 This is a Moo::Role for reading (and then parsing) single data files. It makes the constructor support 3 kinds of file sources:

a path to a readable file
a file handle or anything that can be read like one
a scalar references to content

It also provides an attribute fh that is a handle to the contents of the file argument.

SYNOPSIS

    package MyClassThatDoesStuffWithAFile;

    sub parse {
        my $self = shift;

        # ... do stuff, $self->fh available
    }

    with "File::Parser::Role";

And then in some nearby code:

    my $obj = MyClassThatDoesStuffWithAFile->new("some_file.txt");
    # or #
    my $obj = MyClassThatDoesStuffWithAFile->new(file => "some_file.txt");

    ## and with encoding:
    my $obj = MyClassThatDoesStuffWithAFile->new( file => "some_file.txt", encoding => "utf8" );
    ## encoding can be anything that binmode's encoding() can understand.

    print $obj->filename; # "some_file.txt"
    print $obj->size;     # size of some_file.txt

    ## - OR -

    my $fh = IO::File->new( "< some_file.txt" );
    ## you are responsible for encoding on this handle!

    my $obj = MyClassThatDoesStuffWithAFile->new( file => $fh );

    ## no filename nor file size available

    ## - OR -

    my $file_content = slurp_file( "some_file.txt" );
    my $obj = MyClassThatDoesStuffWithAFile->new( file => \$file_content );

    ## you are also responsible for encoding on this data
    ## no file name nor file size available

DESCRIPTION

This role provides all the bare necessities, and then some, that you expect when you want to parse or otherwise handle a chunk of content typically provided as a file.

It is motivated by, and ideal for, objects that parse files.

INTERFACE

new

The constructor is meant to be all expected kinds of flexible:

  • new("file") # a local filename

  • new($fh)

  • new( file => "file", encoding => "utf8" ); # also works with: new({ ... })

  • new( \"some content" )

The constructor tests the argument to see if it's a path to a local file. If so, it records its filename and size in those two attributes.

It stringifies the file argument for this check, allowing file objects that stringify to paths to work correctly. This applies among others to Path::Tiny.

If a reference or an object is passed as the argument, (that does not stringity fo a readable local file), it is assumed to be something that can be read with the <> operator and passed unchanged to the fh attribute of the class.

fh

Returns ro handle (IO::File for files, IO::String for content) to the contents of the input, be it a file or a sclar reference or an already opened file handle

If the input argument is assumed to be a readable handle to content, it is passed straight through with this method.

parse

A required method that you must write! It is run in BUILD

DIAGNOSTICS

Cannot work with input file

The file argument is neither an existing file, an object nor a reference to content

DEPENDENCIES

INCOMPATIBILITIES

None reported.

REPOSITORY

https://github.com/torbjorn/File-Parser-Role

BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to bug-file-parser-role@rt.cpan.org, or through the web interface at http://rt.cpan.org.

AUTHOR

Torbjørn Lindahl <torbjorn.lindahl@gmail.com>

LICENCE AND COPYRIGHT

Copyright (c) 2012, Torbjørn Lindahl <torbjorn.lindahl@gmail.com>. All rights reserved.

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

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.