
Tie::Slurp - tie a scalar to a named file

use Tie::Slurp;
tie my $template => Tie::Slurp::ReadOnly => 'template';
tie my $output => Tie::Slurp => 'output';
($output = $template) =~ s/\[(\w+)\]/$data{$1}/g;

Tie::Slurp associates a scalar with a named file. Read the scalar, the file is read. Write to the scalar, the file gets clobbered. Flock is used for collision avoidance.
Tie::Slurp::ReadOnly works the same as Tie::Slurp, except that STORE croaks.

Though Tie::Slurp does flock, it still has a 'race condition' problem in many cases. When you do something to the STOREed data, the tied file opens and locks first to FETCH, and closes here once. Then the FETCHed data gets changed, and the file opens and locks again to STORE them. So, if someone access the file while you are changing your data, something unwanted may happen. Tie::Slurp is useful, but don't use it under severe conditions.

David Nicol, <davidnico@cpan.org>

Kenichi Ishigaki, <ishigaki@cpan.org>

Abigail's comparison of slurping idioms on p5p, October 2003

GPL/AL