NAME

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

VERSION

0.30

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';

SUBROUTINES AND VARIABLES

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

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

CONFIGURATION

There are eight 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" will customize the default values of the configuration parameters, so that subsequent file operations are using those defaults.

You use "set" to set the 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.

Customized defaults are limited to the namespace in which you set them.

3. USE STATEMENT

Another way to customize 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 those settings will give rise to.

Note that customized defaults are limited to the namespace in which you set them.

4. DEFAULT CONFIGURATION

The Takeput defaults are:

create: undef (false)

error: undef

exclusive: undef (false)

flatten: undef (false)

newline: undef

patience: 0

separator: $/ (at compile time)

unique: undef (false)

ERROR HANDLING

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

That said, you have the option of changing how runtime errors are handled, by using the "error" configuration parameter.

DEPENDENCIES

Cwd

Exporter

Fcntl

File::Basename

Scalar::Util

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