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.0 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 a method "fh" that gives an at least readable file handle to the contents of the file.
SYNOPSIS
package MyClassThatDoesStuffToAFile;
sub parse { my $self = shift;
# ... do stuff, $self->fh available
}
with "File::Parser::Role";
## ... and in some nearby code:
my $obj = MyClassThatDoesStuffToAFile->new("some_file.txt"); # or # my $obj = MyClassThatDoesStuffToAFile->new(file => "some_file.txt"); ## optinally:
my $obj = MyClassThatDoesStuffToAFile->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 now responsible for encoding on this handle!
my $obj = MyClassThatDoesStuffToAFile->new( file => $fh );
## no filename nor file size available
## - OR -
my $file_content = read_file( "some_file.txt" ); my $obj = MyClassThatDoesStuffToAFile->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")
new($fh)
new( file => "file", encoding => "utf8" ); # also works with:
new({ ... })
new( \"some content" )
Construction tests the argument and if it's a path to a file reachable on the local file system records its filename
and size
in those two attributes.
When it checks if the argument if a local filename, it checks "$file", allowing objects that stringify to paths to work correctly. This applies among others to Path::Tiny.
If a reference to something is passed (or an object), it is assumed to be something that can be read with <> and passed for a handle.
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.
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.