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.
Flock
Tie::Slurp::ReadOnly works the same as Tie::Slurp, except that STORE croaks.
STORE
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.
flock
FETCH
David Nicol, <davidnico@cpan.org>
Kenichi Ishigaki, <ishigaki@cpan.org>
File::Slurp
Abigail's comparison of slurping idioms on p5p, October 2003
perlopentut
GPL/AL
To install Tie::Slurp, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tie::Slurp
CPAN shell
perl -MCPAN -e shell install Tie::Slurp
For more information on module installation, please visit the detailed CPAN module installation guide.