Text-Parts

 view release on metacpan or  search on metacpan

lib/Text/Parts.pm  view on Meta::CPAN

2nd part:
 3333

3rd part:
 4444

At first, C<split> method tries to split by bytes of file size / 3,
Secondly, tries to split by bytes of rest file size / the number of rest part.
So that:

 1st part : 36 bytes / 3 = 12 byte + bytes to line end(if needed)
 2nd part : (36 - 26 bytes) / 2 = 5 byte + bytes to line end(if needed)
 last part: rest part of file

=head1 METHODS

=head2 new

 $s = Text::Parts->new(file => $filename);
 $s = Text::Parts->new(file => $filename, parser => Text::CSV_XS->new({binary => 1}));

Constructor. It can take following options:

=head3 num

number how many you want to split.

=head3 size

file size how much you want to split.
This value is used for calculating C<num>.
If file size is 100 and this value is 25, C<num> is 4.

=head3 file

target file which you want to split.

=head3 parser

Pass parser object(like Text::CSV_XS->new()).
The object must have method which takes filehandle and whose name is C<getline> as default.
If the object's method is different name, pass the name to C<parser_method> option.

=head3 parser_method

name of parser's method. default is C<getline>.

=head3 check_line_start

If this options is true, check line start and move to this position before C<< <$fh> >> or parser's C<getline>/C<parser_method>.
It may be useful when parser's C<getline>/C<parser_method> method doesn't work correctly when parsing wrong format.

default value is 0.

=head3 no_open

If this option is true, don't open file on creating Text::Parts::Part object.
You need to call C<open_and_seek> method from the object when you read the file
(But, C<all> and C<write_file> checks this option, so you don't need to call C<open_and_seek>).

This option is required when you pass too much number, which is more than OS's open file limit, to split method.

=head2 file

 my $file = $s->file;
 $s->file($filename);

get/set target file.

=head2 parser

 my $parser_object = $s->parser;
 $s->parser($parser_object);

get/set parser object.

=head2 parser_method

 my $method = $s->parser_method;
 $s->parser_method($method);

get/set parser method.


=head2 split

 my @parts = $s->split(num => $num);
 my @parts = $s->split(size => $size);
 my @parts = $s->split(num => $num, max_num => 3);

Try to split target file to C<$num> of parts. The returned value is array of Text::Parts::Part object.
If you pass C<< size => bytes >>, calculate C<$num> from file size / C<$size>.

This method doesn't actually split file, only calculate the start and end position of parts.

This returns array of Text::Parts::Part object.
See L</"Text::Parts::Part METHODS">.

If you set max_num, only split number of max_num.

 my @parts = $s->split(num => 5, max_num => 2);

This tries to split 5 parts, but only 2 parts are returned.
This is useful to try to test a few parts of too many parts.

=head2 eol

 my $eol = $s->eol;
 $s->eol($eol);

get/set end of line string. default value is $/.

=head2 write_files

 @filenames = $s->write_files('path/to/name%d.txt', num => 4);

C<name_format> is the format of filename. %d is replaced by number.
For example:

 path/to/name1.txt
 path/to/name2.txt



( run in 1.489 second using v1.01-cache-2.11-cpan-71847e10f99 )