NAME

Sweet::File

SYNOPSIS

use Sweet::File;

my $file1 = Sweet::File->new(
    dir => '/path/to/dir',
    name => 'foo',
);

my $file2 = Sweet::File->new(path => '/path/to/file');

ATTRIBUTES

dir

Instance of Sweet::Dir. If not provided, depends on "path".

encoding

Defaults to utf8.

extension

name

A string containing the file name. If not provided, depends on "path".

name_without_extension

path

Instance of Path::Class::File. If not provided, depends on "dir" and "name".

PRIVATE ATTRIBUTES

_lines

METHODS

append

Append lines to a file.

my @lines = ('first appended line', 'second appended line');

$file->append(\@lines);

copy_to_dir

Copy file to a directory.

$file->copy_to_dir($dir);

Coerces path to Sweet::Dir.

$file->copy_to_dir('/path/to/dir');

Coerces ArrayRef to Sweet::Dir.

$file->copy_to_dir(['/path/to', 'dir']);

does_not_exists

The negation of the -e flag in natural language.

erase

Removes file, using File::Remove.

$file->erase

has_zero_size

The -z flag in natural language.

$file->has_zero_size

is_a_plain_file

The -f flag in natural language.

$file->is_a_plain_file

is_executable

The -x flag in natural language.

$file->is_executable

is_writable

The -w flag in natural language.

$file->is_writable

line

Returns the nth line.

my $line1 = $file->line(0);
my $line2 = $file->line(1);
my $line3 = $file->line(2);

lines

for my $line ( $file->lines ) {
    $line =~ s/foo/bar/;
    say $line;
}

move_to_dir

Move file to a directory.

$file->move_to_dir($dir);

It is just a shortcut to

$file->copy_to_dir($dir) && $file->erase;

num_lines

say $file->num_lines if $file->is_a_plain_file;

split_line

Get first line splitted on pipe.

my @parts = $file->split_line->('|')->(0);

Split lines on comma.

my $splitted_line = $file->split_line->(',');
my @parts0 = $splitted_line->(0);
my @parts1 = $splitted_line->(1);

write

Write lines to a brand new file.

my @lines = ('first line', 'second line');

my $file = Sweet::File->new(
    name => 'brand_new_file.txt',
    dir => $dir,
    lines => \@lines,
);

$file->write;

PRIVATE METHODS

_build_lines

The "lines" builder. To be overridden in subclasses, if needed. It opens a filehandle, put it in an array, chomp it and returns the array reference.

_build_dir

The "dir" builder. To be overridden in subclasses, if needed.

_build_name

The "name" builder. To be overridden in subclasses, if needed.

_build_extension

The "extension" builder. To be overridden in subclasses, if needed.

_build_path

The "path" builder. To be overridden in subclasses, if needed.