File::Slurp says in its description: Efficient Reading / Writing of Complete Files.
I found that it is less efficient than simply opening / closeing a file the normal way:
use Benchmark qw(:all);
use File::Slurp;
my $file1 = 'file1.txt';
my $file2 = 'file2.txt';
my $count = 1000;
cmpthese($count, {
'File1' => \&file1,
'File2' => \&file2,
});
sub file1{
my @array = ( 1..100 );
foreach ( @array ){
open ( FILE1, ">>$file1" );
print FILE1 "$_\n";
close FILE1;
}
}
sub file2{
my @array = ( 1..100 );
foreach ( @array ){
append_file( $file2, "$_\n" );
}
}
Rate File2 File1
File2 3.24/s -- -8%
File1 3.51/s 8% --
I noticed this when writing a log file using File::Slurp that seemed to be taking longer than when I didn't use it.