The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Tie::Slurp::Cached - slurps with locks a la perltie

SYNOPSIS

  use Tie::Slurp::Cached;

  # croak immediately if locked
  $Tie::Slurp::Cached::NoBlocking = 1;

  # tie (and open/lock) files
  tie my $template => 'Tie::Slurp::Cached::ReadOnly' => 'template.html';
  tie my $output   => 'Tie::Slurp::Cached'           => 'output.html';

  # do some operations
  ($output = $template) =~ s/\[(\w+)\]/$data{$1}/g;

  # untie to save/close/unlock
  untie $output;

  # $template would be closed/unlocked implicitly at destroy time.

DESCRIPTION

Tie::Slurp::Cached works almost the same as Tie::Slurp. But, with this module, the specified file opens (and locks) at tie time to read/cache the contents (if any). When you do something to the tied scalar, the cached contents vary as you expect, without any file accesses. When you finish necessary operations, untie the scalar to save the changed contents to the file. If you forget (or are too lazy) to untie, Tie::Slurp::Cached implicitly saves them (and then, closes the file if appropriate) at DESTROY time.

As Tie::Slurp::Cached keeps an exclusive lock while tie-ing, 'race condition' problem doesn't occur (er, basically). You can use this more safely (see below) to implement an incremental counter, or to apply several changes to a file, than Tie::Slurp.

Tie::Slurp::Cached::ReadOnly works almost the same as Tie::Slurp::Cached. However, you can't change the contents through the ReadOnly-tied scalar, and the ReadOnly's lock is not exclusive. You can't write while someone's tie-ing (either Writable or ReadOnly), but you can read while someone's ReadOnly tie-ing, just as you expect.

CONFIGURATION

$Tie::Slurp::Cached::NoBlocking, $Tie::Slurp::Cached::ReadOnly::NoBlocking

These variables change the lock option. If set true, LOCK_NB will be added for *future* locks. The default is undef.

$Tie::Slurp::Cached::WriteFirst

By default, this module might lose previously saved contents in a very unfortunate condition. This is because it truncate()s before syswrite()s. If this variable is set true, it syswrite()s first, then truncate()s the unwanted part. The default is undef.

$Tie::Slurp::Cached::DontSaveAtDestroyTime

By default, this module saves the contents at DESTROY time. But in some cases, you might want to disable this feature, especially if you want to save (commit) your changes only when you have no errors in between. If set true, it won't save at DESTROY time. The default is undef.

SEE ALSO

Tie::Slurp

perltie

AUTHOR

Kenichi Ishigaki, <ishigaki@cpan.org>

COPYRIGHT

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.