NAME

File::Takeput - Slurp style file IO with locking.

VERSION

0.10

SYNOPSIS

use File::Takeput;

# Lock some file and read its content.
my @content1 = take('some_file_name.csv');

# Read content of some other file.
# Retry for up to 2.5 seconds if it is already locked.
my @content2 = grab('some_other_file_name.log' , patience => 2.5);

# Append some data to that other file.
append('some_other_file_name.log')->(@some_data);

# Read content of some third file as a single string.
my ($content3) = grab('some_third_file_name.html' , separator => undef);

# Write content back to the first file after editing it.
# The locks will be released right afterwards.
$content1[$_] =~ s/,/;/g for (0..$#content1);
put('some_file_name.csv')->(@content1);

DESCRIPTION

Slurp style file IO with locking. The purpose of Takeput is to make it pleasant for you to script file IO. Slurp style is both user friendly and very effective, if you can have your files in memory.

The other major point of Takeput is locking. Takeput is careful to help your script be a good citizen in a busy filesystem. All its file operations respect and set flock locking. If your script misses a lock and does not release it, the lock will be released when your script terminates.

Encoding is often part of file IO operations, but Takeput keeps out of that. It reads and writes file content just as strings of bytes, in a sort of line-based binmode. Use some other module if you need decoding and encoding. For example:

use File::Takeput;
use Encode;

my @article = map {decode('iso-8859-1',$_)} grab 'article.latin-1';

ERROR HANDLING

Takeput will die on compile-time errors, but not on runtime errors. In case of a runtime error it might issue a warning. But it will always write an error message in $@ and return an error value.

That said, you are able to change how runtime errors are handled, by using the "error" configuration parameter.

SUBROUTINES AND VARIABLES

Imported by default: append, grab, pass, plunk, put, take

Imported on demand: fgrab, fpass, ftake, reset, set

CONFIGURATION

There are six configuration parameters.

CONFIGURATION OPTIONS

You have a number of options for setting the configuration parameters.

If a parameter is set in more than one way, the most specific setting wins out. In the list above, the item with the lowest number wins out.

1. OPTIONAL NAMED PARAMETERS

All the file operation subroutines can take the configuration parameters as optional named parameters. That means that you can set them per call. The place to write them is after the filename parameter. Example:

my @text = grab 'windows_file.txt' , separator => "\r\n" , newline => "\n";

2. SET AND RESET SUBROUTINES

The two subroutines "set" and "reset" is one way to set the default values for the configuration parameters. You use "set" to set custom values, and "reset" to set the values back to the Takeput defaults.

Think of it as assignment statements. If there are multiple calls, the last one is the one that is in effect.

3. USE STATEMENT

Another way to set the default values is in the use statement that imports Takeput. For example:

use File::Takeput separator => "\n";

When you do it like this, the values are set at compile-time. Because of that, Takeput will die on any errors that these settings will give.

4. DEFAULT CONFIGURATION

The Takeput defaults are:

create: undef (false)

error: undef

exclusive: undef (false)

newline: undef

patience: 0

separator: $/ (at compile time)

DEPENDENCIES

Cwd

Fcntl

File::Basename

Time::HiRes

KNOWN ISSUES

No known issues.

TODO

Decide on empty string "separator". It ought to give a list of bytes, but readline gives an unintuitive list. It would be a mess with the line ending transformations.

An empty string will be an invalid value for now.

SEE ALSO

Encode

File::Slurp

File::Slurper

LICENSE & COPYRIGHT

(c) 2023 Bjørn Hee

Licensed under the Apache License, version 2.0

https://www.apache.org/licenses/LICENSE-2.0.txt