NAME

Treex::Core::Files - helper class for iterating over filenames

VERSION

version 2.20160629

SYNOPSIS

package My::Class;
use Moose;

has from => (
    is => 'ro',
    isa => 'Treex::Core::Files',
    coerce => 1,
    handles => [qw(next_filename current_filename)],
);

# and then
my $c = My::Class(from=>'f1.txt f2.txt.gz @my.filelist');

while (defined (my $filename = $c->next_filename)){ ... }
#or
while (my $filehandle = $c->next_filehandle){ ... }

# You can use also wildcard expansion
my $c = My::Class(from=>'!dir??/file*.txt');

DESCRIPTION

The @filelist and !wildcard conventions are used in several tools, e.g. 7z or javac. For a large number of files, list the file names in a file - one per line. Then use the list file name preceded by an @ character.

Methods <next_*> serve as iterators and return undef if the called after the last file is reached.

METHODS

number_of_files

Returns the total number of files contained by this instance.

file_number

Returns ordinal number (1..number_of_files) of the current file.

current_filename

Returns the current filename or undef if the iterator is before the first file (i.e. next_filename has not been called so far) or after the last file.

next_filename

Returns the next filename (and increments the file_number).

current_filehandle

Opens the current file for reading and returns the filehandle. Filename "-" is interpreted as STDIN. Filenames with extension ".gz" are opened via PerlIO::via::gzip (ie. unzipped on the fly).

next_filehandle

Returns the next filehandle (and increments the file_number).

next_file_text

Returns the content of the next file (slurp) and increments the file_number.

next_line

Returns the next line of the current file. If the end of file is reached and attribute join_files_for_next_line is set to true (which is by default), the first line of next file is returned (and file_number incremented).

get_hash

Returns MD5 hash computed from the filenames and last modify times.

$filenames_ref = string_to_filenames($string)

Helper method that expands comma-or-space-separated list of filenames and returns an array reference containing the filenames. If the string starts with "!", it is interpreted as wildcards (see Perl glob). If a filename starts with "@" it is interpreted as a file list with one filename per line.

AUTHOR

Martin Popel <popel@ufal.mff.cuni.cz>

COPYRIGHT AND LICENSE

Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague

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