Text-Parts

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

2nd part:
 3333

3rd part:
 4444

At first, `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

# METHODS

## 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:

### num

number how many you want to split.

### size

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

### file

target file which you want to split.

### parser

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

### parser\_method

name of parser's method. default is `getline`.

### check\_line\_start

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

default value is 0.

### no\_open

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

## file

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

get/set target file.

## parser

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

get/set parser object.

## parser\_method

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

get/set parser method.



## 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 `$num` of parts. The returned value is array of Text::Parts::Part object.
If you pass `size => bytes`, calculate `$num` from file size / `$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 ["Text::Parts::Part METHODS"](#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.

## eol

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

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

## write\_files

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

`name_format` is the format of filename. %d is replaced by number.
For example:

    path/to/name1.txt



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