| 1 | package Treex::Core::Files; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 85 | $Treex::Core::Files::VERSION = '0.08633_1'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | 85 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 85 | ▶ | use MooseX::SemiAffordanceAccessor 0.09; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 85 | ▶ | use Treex::Core::Log; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 85 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 85 | ▶ | use File::Slurp 9999.19; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 85 | has filenames => ( | |||||||||||||||||||||||||||
| 12 | is => 'ro', | ||||||||||||||||||||||||||||
| 13 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||
| 14 | writer => '_set_filenames', | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 85 | has file_number => ( | |||||||||||||||||||||||||||
| 18 | isa => 'Int', | ||||||||||||||||||||||||||||
| 19 | is => 'ro', | ||||||||||||||||||||||||||||
| 20 | writer => '_set_file_number', | ||||||||||||||||||||||||||||
| 21 | default => 0, | ||||||||||||||||||||||||||||
| 22 | init_arg => undef, | ||||||||||||||||||||||||||||
| 23 | documentation => 'Number of input files loaded so far.', | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub BUILD { | ||||||||||||||||||||||||||||
| 27 | 33 | ▶ | my ( $self, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | 33 | ▶ | return if $args->{filenames}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 33 | ▶ | log_fatal 'parameter "string" is required' if !defined $args->{string}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 33 | $self->_set_filenames( $self->string_to_filenames( $args->{string} ) ); | |||||||||||||||||||||||||||
| 31 | 33 | return; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub string_to_filenames { | ||||||||||||||||||||||||||||
| 35 | 33 | ▶ | my ( $self, $string ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
string_to_filenames: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | 33 | return [ map { $self->_token_to_filenames($_) } split /[ ,]+/, $string ]; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub _token_to_filenames { | ||||||||||||||||||||||||||||
| 40 | 36 | ▶ | my ( $self, $token ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_token_to_filenames: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 41 | 36 | ▶ | return $token if $token !~ s/^@(.*)/$1/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 1 | ▶ | if ( $token eq '-' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | $token = \*STDIN; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 1 | my @filenames = read_file( $token, chomp => 1 ); | |||||||||||||||||||||||||||
| 46 | 1 | return @filenames; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | sub number_of_files { | ||||||||||||||||||||||||||||
| 50 | 83 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
number_of_files: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | 83 | return scalar @{ $self->filenames }; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub current_filename { | ||||||||||||||||||||||||||||
| 55 | 60 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
current_filename: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 60 | ▶ | return if $self->file_number == 0 || $self->file_number > @{ $self->filenames }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->file_number == 0 or $self->file_number > @{$self->filenames;}
MC/DC0%0/2
$self->file_number == 0 or $self->file_number > @{$self->filenames;}
$self->file_number == 0
$self->file_number > @{$self->filenames;}
Truth table33%1/3
A or B
A= $self->file_number == 0B= $self->file_number > @{$self->filenames;}
| |||||||||||||||||||||||||||||
| 57 | 60 | return $self->filenames->[ $self->file_number - 1 ]; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub next_filename { | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
next_filename: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 0 | $self->_set_file_number( $self->file_number + 1 ); | |||||||||||||||||||||||||||
| 63 | 0 | return $self->current_filename(); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | #<<< | ||||||||||||||||||||||||||||
| 67 | 85 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | coerce 'Treex::Core::Files' | ||||||||||||||||||||||||||||
| 69 | => from 'Str' | ||||||||||||||||||||||||||||
| 70 | 32 | ▶ | => via { Treex::Core::Files->new( string => $_ ) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | => from 'ArrayRef[Str]' | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | => via { Treex::Core::Files->new( filenames => $_ ) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 73 | #>>> | ||||||||||||||||||||||||||||
| 74 | # TODO: POD, next_filehandle, gz support | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 85 | 1; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | __END__ | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head1 NAME | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Treex::Core::Files - helper class for iterating over filenames | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head1 VERSION | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | version 0.08633_1 | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | package My::Class; | ||||||||||||||||||||||||||||
| 91 | use Moose; | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | has from => ( | ||||||||||||||||||||||||||||
| 94 | is => 'ro', | ||||||||||||||||||||||||||||
| 95 | isa => 'Treex::Core::Files', | ||||||||||||||||||||||||||||
| 96 | coerce => 1, | ||||||||||||||||||||||||||||
| 97 | handles => [qw(next_filename current_filename)], | ||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # and then | ||||||||||||||||||||||||||||
| 101 | my $c = My::Class(from=>'f1.txt f2.txt.gz @my.filelist'); | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | while (defined (my $filename = $c->next_filename)){ ... } | ||||||||||||||||||||||||||||
| 104 | #or | ||||||||||||||||||||||||||||
| 105 | while (my $filehandle = $c->next_filehandle){ ... } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | TODO | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | I<@filelist> convention is used in several tools, e.g. javac or 7z. | ||||||||||||||||||||||||||||
| 112 | For a large number of files, list the the file names in a file - one per line. | ||||||||||||||||||||||||||||
| 113 | Then use the list file name preceded by an @ character. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Martin Popel <popel@ufal.mff.cuni.cz> | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||