Commit 336e6624 authored by root's avatar root

fixed deprecated sysread() on utf8 handles

parent 0e7b2053
......@@ -162,7 +162,7 @@ ExtUtils::CBuilder 0.280205
File::Listing 6.04
File::Path 2.09|--notest|--force
File::ShareDir 1.03
File::Slurp 9999.19
File::Slurp 9999.32
File::Spec::Native 1.003
File::chdir 0.1008
Filesys::Notify::Simple 0.08
......
package File::Slurp;
use 5.6.2 ;
use strict;
use warnings ;
our $VERSION = '9999.32';
$VERSION = eval $VERSION;
use Carp ;
use Exporter ;
use Exporter qw(import);
use Fcntl qw( :DEFAULT ) ;
use File::Basename ();
use File::Spec;
use File::Temp qw(tempfile);
use IO::Handle ();
use POSIX qw( :fcntl_h ) ;
use Errno ;
#use Symbol ;
use vars qw( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION ) ;
@ISA = qw( Exporter ) ;
$VERSION = '9999.19';
my @std_export = qw(
read_file
......@@ -30,263 +29,117 @@ my @edit_export = qw(
edit_file_lines
) ;
my @ok_export = qw(
my @abbrev_export = qw(
rf
wf
ef
efl
) ;
@EXPORT_OK = (
our @EXPORT_OK = (
@edit_export,
@abbrev_export,
qw(
slurp
prepend_file
),
) ;
%EXPORT_TAGS = (
'all' => [ @std_export, @edit_export, @EXPORT_OK ],
our %EXPORT_TAGS = (
'all' => [ @std_export, @edit_export, @abbrev_export, @EXPORT_OK ],
'edit' => [ @edit_export ],
'std' => [ @std_export ],
'abr' => [ @abbrev_export ],
) ;
@EXPORT = @std_export ;
our @EXPORT = @std_export ;
my $max_fast_slurp_size = 1024 * 100 ;
my $is_win32 = $^O =~ /win32/i ;
# Install subs for various constants that aren't set in older perls
# (< 5.005). Fcntl on old perls uses Exporter to define subs without a
# () prototype These can't be overridden with the constant pragma or
# we get a prototype mismatch. Hence this less than aesthetically
# appealing BEGIN block:
BEGIN {
unless( defined &SEEK_SET ) {
*SEEK_SET = sub { 0 };
*SEEK_CUR = sub { 1 };
*SEEK_END = sub { 2 };
}
unless( defined &O_BINARY ) {
*O_BINARY = sub { 0 };
*O_RDONLY = sub { 0 };
*O_WRONLY = sub { 1 };
}
unless ( defined &O_APPEND ) {
if ( $^O =~ /olaris/ ) {
*O_APPEND = sub { 8 };
*O_CREAT = sub { 256 };
*O_EXCL = sub { 1024 };
}
elsif ( $^O =~ /inux/ ) {
*O_APPEND = sub { 1024 };
*O_CREAT = sub { 64 };
*O_EXCL = sub { 128 };
}
elsif ( $^O =~ /BSD/i ) {
*O_APPEND = sub { 8 };
*O_CREAT = sub { 512 };
*O_EXCL = sub { 2048 };
}
}
}
# print "OS [$^O]\n" ;
# print "O_BINARY = ", O_BINARY(), "\n" ;
# print "O_RDONLY = ", O_RDONLY(), "\n" ;
# print "O_WRONLY = ", O_WRONLY(), "\n" ;
# print "O_APPEND = ", O_APPEND(), "\n" ;
# print "O_CREAT ", O_CREAT(), "\n" ;
# print "O_EXCL ", O_EXCL(), "\n" ;
*slurp = \&read_file ;
*rf = \&read_file ;
sub read_file {
my $file_name = shift ;
my $opts = ( ref $_[0] eq 'HASH' ) ? shift : { @_ } ;
# this is the optimized read_file for shorter files.
# the test for -s > 0 is to allow pseudo files to be read with the
# regular loop since they return a size of 0.
if ( !ref $file_name && -e $file_name && -s _ > 0 &&
-s _ < $max_fast_slurp_size && !%{$opts} && !wantarray ) {
my $fh ;
unless( sysopen( $fh, $file_name, O_RDONLY ) ) {
@_ = ( $opts, "read_file '$file_name' - sysopen: $!");
goto &_error ;
my $file_name = shift;
my $opts = (ref $_[0] eq 'HASH') ? shift : {@_};
# options we care about:
# array_ref binmode blk_size buf_ref chomp err_mode scalar_ref
# let's see if we have a stringified object before doing anything else
# We then only have to deal with when we are given a file handle/globref
if (ref($file_name)) {
my $ref_result = _check_ref($file_name, $opts);
if (ref($ref_result)) {
@_ = ($opts, $ref_result);
goto &_error;
}
my $read_cnt = sysread( $fh, my $buf, -s _ ) ;
unless ( defined $read_cnt ) {
@_ = ( $opts,
"read_file '$file_name' - small sysread: $!");
goto &_error ;
$file_name = $ref_result if $ref_result;
# we have now stringified $file_name if possible. if it's still a ref
# then we probably have a file handle
}
$buf =~ s/\015\012/\n/g if $is_win32 ;
return $buf ;
}
# set the buffer to either the passed in one or ours and init it to the null
# string
my $buf ;
my $buf_ref = $opts->{'buf_ref'} || \$buf ;
${$buf_ref} = '' ;
my( $read_fh, $size_left, $blk_size ) ;
# deal with ref for a file name
# it could be an open handle or an overloaded object
if ( ref $file_name ) {
my $ref_result = _check_ref( $file_name ) ;
if ( ref $ref_result ) {
# we got an error, deal with it
@_ = ( $opts, $ref_result ) ;
goto &_error ;
}
if ( $ref_result ) {
# we got an overloaded object and the result is the stringified value
# use it as the file name
$file_name = $ref_result ;
my $fh;
if (ref($file_name)) {
$fh = $file_name;
}
else {
# here we have just an open handle. set $read_fh so we don't do a sysopen
$read_fh = $file_name ;
$blk_size = $opts->{'blk_size'} || 1024 * 1024 ;
$size_left = $blk_size ;
# to keep with the old ways, read in :raw by default
unless (open $fh, "<:raw", $file_name) {
@_ = ($opts, "read_file '$file_name' - open: $!");
goto &_error;
}
# even though we set raw, let binmode take place here (busted)
if (my $bm = $opts->{binmode}) {
binmode $fh, $bm;
}
# see if we have a path we need to open
unless ( $read_fh ) {
# a regular file. set the sysopen mode
my $mode = O_RDONLY ;
#printf "RD: BINARY %x MODE %x\n", O_BINARY, $mode ;
$read_fh = local( *FH ) ;
# $read_fh = gensym ;
unless ( sysopen( $read_fh, $file_name, $mode ) ) {
@_ = ( $opts, "read_file '$file_name' - sysopen: $!");
goto &_error ;
}
if ( my $binmode = $opts->{'binmode'} ) {
binmode( $read_fh, $binmode ) ;
}
# get the size of the file for use in the read loop
$size_left = -s $read_fh ;
#print "SIZE $size_left\n" ;
# we need a blk_size if the size is 0 so we can handle pseudofiles like in
# /proc. these show as 0 size but have data to be slurped.
unless( $size_left ) {
$blk_size = $opts->{'blk_size'} || 1024 * 1024 ;
$size_left = $blk_size ;
}
# we are now sure to have an open file handle. Let's slurp it in the same
# way that File::Slurper does.
my $buf;
my $buf_ref = $opts->{buf_ref} || \$buf;
${$buf_ref} = '';
my $blk_size = $opts->{blk_size} || 1024 * 1024;
if (my $size = -f $fh && -s _) {
$blk_size = $size if $size < $blk_size;
my ($pos, $read) = 0;
do {
unless(defined($read = read $fh, ${$buf_ref}, $blk_size, $pos)) {
@_ = ($opts, "read_file '$file_name' - read: $!");
goto &_error;
}
# infinite read loop. we exit when we are done slurping
while( 1 ) {
# do the read and see how much we got
my $read_cnt = sysread( $read_fh, ${$buf_ref},
$size_left, length ${$buf_ref} ) ;
# since we're using sysread Perl won't automatically restart the call
# when interrupted by a signal.
next if $!{EINTR};
unless ( defined $read_cnt ) {
@_ = ( $opts, "read_file '$file_name' - loop sysread: $!");
goto &_error ;
$pos += $read;
} while ($read && $pos < $size);
}
# good read. see if we hit EOF (nothing left to read)
last if $read_cnt == 0 ;
# loop if we are slurping a handle. we don't track $size_left then.
next if $blk_size ;
# count down how much we read and loop if we have more to read.
$size_left -= $read_cnt ;
last if $size_left <= 0 ;
else {
${$buf_ref} = do { local $/; <$fh> };
}
# fix up cr/lf to be a newline if this is a windows text file
${$buf_ref} =~ s/\015\012/\n/g if $is_win32 && !$opts->{'binmode'} ;
my $sep = $/ ;
$sep = '\n\n+' if defined $sep && $sep eq '' ;
# see if caller wants lines
if( wantarray || $opts->{'array_ref'} ) {
use re 'taint' ;
seek($fh, $opts->{_data_tell}, SEEK_SET) if $opts->{_is_data} && $opts->{_data_tell};
# line endings if we're on Windows
${$buf_ref} =~ s/\015\012/\012/g if ${$buf_ref} && $is_win32 && !$opts->{binmode};
# we now have a buffer filled with the file content. Figure out how to
# return it to the user
my $want_array = wantarray; # let's only ask for this once
if ($want_array || $opts->{array_ref}) {
use re 'taint';
my $sep = $/;
$sep = '\n\n+' if defined $sep && $sep eq '';
# split the buffered content into lines
my @lines = length(${$buf_ref}) ?
${$buf_ref} =~ /(.*?$sep|.+)/sg : () ;
chomp @lines if $opts->{'chomp'} ;
# caller wants an array ref
return \@lines if $opts->{'array_ref'} ;
# caller wants list of lines
return @lines ;
${$buf_ref} =~ /(.*?$sep|.+)/sg : ();
chomp @lines if $opts->{chomp};
return \@lines if $opts->{array_ref};
return @lines;
}
# caller wants a scalar ref to the slurped text
return $buf_ref if $opts->{'scalar_ref'} ;
# caller wants a scalar with the slurped text (normal scalar context)
return ${$buf_ref} if defined wantarray ;
# caller passed in an i/o buffer by reference (normal void context)
return ;
return $buf_ref if $opts->{scalar_ref};
# if the function was called in scalar context, return the contents
return ${$buf_ref} if defined $want_array;
# if we were called in void context, return nothing
return;
}
# errors in this sub are returned as scalar refs
......@@ -295,7 +148,7 @@ sub read_file {
sub _check_ref {
my( $handle ) = @_ ;
my( $handle, $opts ) = @_ ;
# check if we are reading from a handle (GLOB or IO object)
......@@ -303,7 +156,7 @@ sub _check_ref {
# we have a handle. deal with seeking to it if it is DATA
my $err = _seek_data_handle( $handle ) ;
my $err = _seek_data_handle( $handle, $opts ) ;
# return the error string if any
......@@ -328,7 +181,10 @@ sub _check_ref {
sub _seek_data_handle {
my( $handle ) = @_ ;
my( $handle, $opts ) = @_ ;
# store some meta-data about the __DATA__ file handle
$opts->{_is_data} = 0;
$opts->{_data_tell} = 0;
# DEEP DARK MAGIC. this checks the UNTAINT IO flag of a
# glob/handle. only the DATA handle is untainted (since it is from
......@@ -350,11 +206,18 @@ ERR
if ( B::svref_2object( $handle )->IO->IoFLAGS & 16 ) {
# we now know we have the data handle. Let's store its original
# location in the file so that we can put it back after the read.
# this is only done for Bugwards-compatibility in some dists such as
# CPAN::Index::API that made use of the oddity where sysread was in use
# before
$opts->{_is_data} = 1;
$opts->{_data_tell} = tell($handle);
# set the seek position to the current tell.
unless( sysseek( $handle, tell( $handle ), SEEK_SET ) ) {
return "read_file '$handle' - sysseek: $!" ;
}
# unless( sysseek( $handle, tell( $handle ), SEEK_SET ) ) {
# return "read_file '$handle' - sysseek: $!" ;
# }
}
# seek was successful, return no error string
......@@ -362,190 +225,118 @@ ERR
return ;
}
*wf = \&write_file ;
sub write_file {
my $file_name = shift ;
# get the optional argument hash ref from @_ or an empty hash ref.
my $opts = ( ref $_[0] eq 'HASH' ) ? shift : {} ;
my( $buf_ref, $write_fh, $no_truncate, $orig_file_name, $data_is_ref ) ;
# get the buffer ref - it depends on how the data is passed into write_file
# after this if/else $buf_ref will have a scalar ref to the data.
if ( ref $opts->{'buf_ref'} eq 'SCALAR' ) {
# a scalar ref passed in %opts has the data
# note that the data was passed by ref
$buf_ref = $opts->{'buf_ref'} ;
$data_is_ref = 1 ;
}
elsif ( ref $_[0] eq 'SCALAR' ) {
# the first value in @_ is the scalar ref to the data
# note that the data was passed by ref
$buf_ref = shift ;
$data_is_ref = 1 ;
my $file_name = shift;
my $opts = (ref $_[0] eq 'HASH') ? shift : {};
# options we care about:
# append atomic binmode buf_ref err_mode no_clobber perms
my $fh;
my $no_truncate = 0;
my $orig_filename;
# let's see if we have a stringified object or some sort of handle
# or globref before doing anything else
if (ref($file_name)) {
my $ref_result = _check_ref($file_name, $opts);
if (ref($ref_result)) {
# some error happened while checking for a ref
@_ = ($opts, $ref_result);
goto &_error;
}
elsif ( ref $_[0] eq 'ARRAY' ) {
# the first value in @_ is the array ref to the data so join it.
${$buf_ref} = join '', @{$_[0]} ;
if ($ref_result) {
# we have now stringified $file_name from the overloaded obj
$file_name = $ref_result;
}
else {
# good old @_ has all the data so join it.
${$buf_ref} = join '', @_ ;
# we now have a proper handle ref
# make sure we don't call truncate on it
$fh = $file_name;
$no_truncate = 1;
# can't do atomic or permissions on a file handle
delete $opts->{atomic};
delete $opts->{perms};
}
# deal with ref for a file name
if ( ref $file_name ) {
my $ref_result = _check_ref( $file_name ) ;
if ( ref $ref_result ) {
# we got an error, deal with it
@_ = ( $opts, $ref_result ) ;
goto &_error ;
}
if ( $ref_result ) {
# we got an overloaded object and the result is the stringified value
# use it as the file name
$file_name = $ref_result ;
# open the file for writing if we were given a filename
unless ($fh) {
$orig_filename = $file_name;
my $perms = defined($opts->{perms}) ? $opts->{perms} : 0666;
# set the mode for the sysopen
my $mode = O_WRONLY | O_CREAT;
$mode |= O_APPEND if $opts->{append};
$mode |= O_EXCL if $opts->{no_clobber};
if ($opts->{atomic}) {
# in an atomic write, we must open a new file in the same directory
# as the original to account for ACLs. We must also set the new file
# to the same permissions as the original unless overridden by the
# caller's request to set a specified permission set.
my $dir = File::Spec->rel2abs(File::Basename::dirname($file_name));
if (!defined($opts->{perms}) && -e $file_name && -f _) {
$perms = 07777 & (stat $file_name)[2];
}
else {
# we now have a proper handle ref.
# make sure we don't call truncate on it.
$write_fh = $file_name ;
$no_truncate = 1 ;
# we must ensure we're using a good temporary filename (doesn't already
# exist). This is slower, but safer.
{
local $^W = 0; # AYFKM
(undef, $file_name) = tempfile('.tempXXXXX', DIR => $dir, OPEN => 0);
}
}
# see if we have a path we need to open
unless( $write_fh ) {
# spew to regular file.
if ( $opts->{'atomic'} ) {
# in atomic mode, we spew to a temp file so make one and save the original
# file name.
$orig_file_name = $file_name ;
$file_name .= ".$$" ;
$fh = local *FH;
unless (sysopen($fh, $file_name, $mode, $perms)) {
@_ = ($opts, "write_file '$file_name' - sysopen: $!");
goto &_error;
}
# set the mode for the sysopen
my $mode = O_WRONLY | O_CREAT ;
$mode |= O_APPEND if $opts->{'append'} ;
$mode |= O_EXCL if $opts->{'no_clobber'} ;
my $perms = $opts->{perms} ;
$perms = 0666 unless defined $perms ;
#printf "WR: BINARY %x MODE %x\n", O_BINARY, $mode ;
# open the file and handle any error.
$write_fh = local( *FH ) ;
# $write_fh = gensym ;
unless ( sysopen( $write_fh, $file_name, $mode, $perms ) ) {
@_ = ( $opts, "write_file '$file_name' - sysopen: $!");
goto &_error ;
}
# we now have an open file handle as well as data to write to that handle
if (my $binmode = $opts->{binmode}) {
binmode($fh, $binmode);
}
if ( my $binmode = $opts->{'binmode'} ) {
binmode( $write_fh, $binmode ) ;
# get the data to print to the file
# get the buffer ref - it depends on how the data is passed in
# after this if/else $buf_ref will have a scalar ref to the data
my $buf_ref;
my $data_is_ref = 0;
if (ref($opts->{buf_ref}) eq 'SCALAR') {
# a scalar ref passed in %opts has the data
# note that the data was passed by ref
$buf_ref = $opts->{buf_ref};
$data_is_ref = 1;
}
sysseek( $write_fh, 0, SEEK_END ) if $opts->{'append'} ;
#print 'WR before data ', unpack( 'H*', ${$buf_ref}), "\n" ;
# fix up newline to write cr/lf if this is a windows text file
if ( $is_win32 && !$opts->{'binmode'} ) {
# copy the write data if it was passed by ref so we don't clobber the
# caller's data
$buf_ref = \do{ my $copy = ${$buf_ref}; } if $data_is_ref ;
${$buf_ref} =~ s/\n/\015\012/g ;
elsif (ref($_[0]) eq 'SCALAR') {
# the first value in @_ is the scalar ref to the data
# note that the data was passed by ref
$buf_ref = shift;
$data_is_ref = 1;
}
#print 'after data ', unpack( 'H*', ${$buf_ref}), "\n" ;
# get the size of how much we are writing and init the offset into that buffer
my $size_left = length( ${$buf_ref} ) ;
my $offset = 0 ;
# loop until we have no more data left to write
do {
# do the write and track how much we just wrote
my $write_cnt = syswrite( $write_fh, ${$buf_ref},
$size_left, $offset ) ;
# since we're using syswrite Perl won't automatically restart the call
# when interrupted by a signal.
next if $!{EINTR};
unless ( defined $write_cnt ) {
@_ = ( $opts, "write_file '$file_name' - syswrite: $!");
goto &_error ;
elsif (ref($_[0]) eq 'ARRAY') {
# the first value in @_ is the array ref to the data so join it.
${$buf_ref} = join '', @{$_[0]};
}
else {
# good old @_ has all the data so join it.
${$buf_ref} = join '', @_;
}
# track how much left to write and where to write from in the buffer
$size_left -= $write_cnt ;
$offset += $write_cnt ;
} while( $size_left > 0 ) ;
# we truncate regular files in case we overwrite a long file with a shorter file
# so seek to the current position to get it (same as tell()).
truncate( $write_fh,
sysseek( $write_fh, 0, SEEK_CUR ) ) unless $no_truncate ;
close( $write_fh ) ;
# handle the atomic mode - move the temp file to the original filename.
if ( $opts->{'atomic'} && !rename( $file_name, $orig_file_name ) ) {
# seek and print
seek($fh, 0, SEEK_END) if $opts->{append};
print {$fh} ${$buf_ref};
truncate($fh, tell($fh)) unless $no_truncate;
close($fh);
@_ = ( $opts, "write_file '$file_name' - rename: $!" ) ;
goto &_error ;
if ($opts->{atomic} && !rename($file_name, $orig_filename)) {
@_ = ($opts, "write_file '$file_name' - rename: $!");
goto &_error;
}
return 1 ;
return 1;
}
# this is for backwards compatibility with the previous File::Slurp module.
# write_file always overwrites an existing file
*overwrite_file = \&write_file ;
# the current write_file has an append mode so we use that. this
......@@ -631,6 +422,8 @@ sub prepend_file {
# edit a file as a scalar in $_
*ef = \&edit_file ;
sub edit_file(&$;$) {
my( $edit_code, $file_name, $opts ) = @_ ;
......@@ -684,6 +477,8 @@ sub edit_file(&$;$) {
return $write_result ;
}
*efl = \&edit_file_lines ;
sub edit_file_lines(&$;$) {
my( $edit_code, $file_name, $opts ) = @_ ;
......@@ -763,7 +558,7 @@ sub read_dir {
if ( $opts->{'prefix'} ) {
substr( $_, 0, 0, "$dir/" ) for @dir_entries ;
$_ = File::Spec->catfile($dir, $_) for @dir_entries;
}
return @dir_entries if wantarray ;
......@@ -818,34 +613,34 @@ File::Slurp - Simple and Efficient Reading/Writing/Modifying of Complete Files
use File::Slurp;
# read in a whole file into a scalar
my $text = read_file( 'filename' ) ;
# read in a whole file into a scalar
my $text = read_file('/path/file');
# read in a whole file into an array of lines
my @lines = read_file( 'filename' ) ;
# read in a whole file into an array of lines
my @lines = read_file('/path/file');
# write out a whole file from a scalar
write_file( 'filename', $text ) ;
# write out a whole file from a scalar
write_file('/path/file', $text);
# write out a whole file from an array of lines
write_file( 'filename', @lines ) ;
# write out a whole file from an array of lines
write_file('/path/file', @lines);
# Here is a simple and fast way to load and save a simple config file
# made of key=value lines.
my %conf = read_file( $file_name ) =~ /^(\w+)=(.*)$/mg ;
write_file( $file_name, {atomic => 1}, map "$_=$conf{$_}\n", keys %conf ) ;
# Here is a simple and fast way to load and save a simple config file
# made of key=value lines.
my %conf = read_file('/path/file') =~ /^(\w+)=(.*)$/mg;
write_file('/path/file', {atomic => 1}, map "$_=$conf{$_}\n", keys %conf);
# insert text at the beginning of a file
prepend_file( 'filename', $text ) ;
# insert text at the beginning of a file
prepend_file('/path/file', $text);
# in-place edit to replace all 'foo' with 'bar' in file
edit_file { s/foo/bar/g } 'filename' ;
# in-place edit to replace all 'foo' with 'bar' in file
edit_file { s/foo/bar/g } '/path/file';
# in-place edit to delete all lines with 'foo' from file
edit_file_lines sub { $_ = '' if /foo/ }, 'filename' ;
# in-place edit to delete all lines with 'foo' from file
edit_file_lines sub { $_ = '' if /foo/ }, '/path/file';
# read in a whole directory of file names (skipping . and ..)
my @files = read_dir( '/path/to/dir' ) ;
# read in a whole directory of file names (skipping . and ..)
my @files = read_dir('/path/to/dir');
=head1 DESCRIPTION
......@@ -853,409 +648,481 @@ This module provides subs that allow you to read or write entire files
with one simple call. They are designed to be simple to use, have
flexible ways to pass in or get the file contents and to be very
efficient. There is also a sub to read in all the files in a
directory other than C<.> and C<..>
directory.
These slurp/spew subs work for files, pipes and sockets, stdio,
pseudo-files, and the DATA handle. Read more about why slurping files is
a good thing in the file 'slurp_article.pod' in the extras/ directory.
=head2 WARNING - PENDING DOOM
If you are interested in how fast these calls work, check out the
slurp_bench.pl program in the extras/ directory. It compares many
different forms of slurping. You can select the I/O direction, context
and file sizes. Use the --help option to see how to run it.
Although you technically I<can>, do NOT use this module to work on file handles,
pipes, sockets, standard IO, or the C<DATA> handle. These are
features implemented long ago that just really shouldn't be abused here.
=head2 B<read_file>
Be warned: this activity will lead to inaccurate encoding/decoding of data.
This sub reads in an entire file and returns its contents to the
caller. In scalar context it returns the entire file as a single
scalar. In list context it will return a list of lines (using the
current value of $/ as the separator including support for paragraph
mode when it is set to '').
All further mentions of actions on the above have been removed from this
documentation and that feature set will likely be deprecated in the future.
my $text = read_file( 'filename' ) ;
my $bin = read_file( 'filename' { binmode => ':raw' } ) ;
my @lines = read_file( 'filename' ) ;
my $lines = read_file( 'filename', array_ref => 1 ) ;
In other words, if you don't have a filename to pass, consider using the
standard C<< do { local $/; <$fh> } >>, or
L<Data::Section>/L<Data::Section::Simple> for working with C<__DATA__>.
The first argument is the file to slurp in. If the next argument is a
hash reference, then it is used as the options. Otherwise the rest of
the argument list are is used as key/value options.
=head1 FUNCTIONS
If the file argument is a handle (if it is a ref and is an IO or GLOB
object), then that handle is slurped in. This mode is supported so you
slurp handles such as C<DATA> and C<STDIN>. See the test handle.t for
an example that does C<open( '-|' )> and the child process spews data
to the parant which slurps it in. All of the options that control how
the data is returned to the caller still work in this case.
L<File::Slurp> implements the following functions.
If the first argument is an overloaded object then its stringified value
is used for the filename and that file is opened. This is a new feature
in 9999.14. See the stringify.t test for an example.
=head2 append_file
By default C<read_file> returns an undef in scalar contex or a single
undef in list context if it encounters an error. Those are both
impossible to get with a clean read_file call which means you can check
the return value and always know if you had an error. You can change how
errors are handled with the C<err_mode> option.
use File::Slurp qw(append_file write_file);
my $res = append_file('/path/file', "Some text");
# same as
my $res = write_file('/path/file', {append => 1}, "Some text");
Speed Note: If you call read_file and just get a scalar return value
it is now optimized to handle shorter files. This is only used if no
options are used, the file is shorter then 100k bytes, the filename is
a plain scalar and a scalar file is returned. If you want the fastest
slurping, use the C<buf_ref> or C<scalar_ref> options (see below)
The C<append_file> function is simply a synonym for the
L<File::Slurp/"write_file"> function, but ensures that the C<append> option is
set.
NOTE: as of version 9999.06, read_file works correctly on the C<DATA>
handle. It used to need a sysseek workaround but that is now handled
when needed by the module itself.
=head2 edit_file
You can optionally request that C<slurp()> is exported to your code. This
is an alias for read_file and is meant to be forward compatible with
Perl 6 (which will have slurp() built-in).
use File::Slurp qw(edit_file);
# perl -0777 -pi -e 's/foo/bar/g' /path/file
edit_file { s/foo/bar/g } '/path/file';
edit_file sub { s/foo/bar/g }, '/path/file';
sub replace_foo { s/foo/bar/g }
edit_file \&replace_foo, '/path/file';
The options for C<read_file> are:
The C<edit_file> function reads in a file into C<$_>, executes a code block that
should modify C<$_>, and then writes C<$_> back to the file. The C<edit_file>
function reads in the entire file and calls the code block one time. It is
equivalent to the C<-pi> command line options of Perl but you can call it from
inside your program and not have to fork out a process.
=head3 binmode
The first argument to C<edit_file> is a code block or a code reference. The
code block is not followed by a comma (as with C<grep> and C<map>) but a code
reference is followed by a comma.
If you set the binmode option, then its value is passed to a call to
binmode on the opened handle. You can use this to set the file to be
read in binary mode, utf8, etc. See perldoc -f binmode for more.
The next argument is the filename.
my $bin_data = read_file( $bin_file, binmode => ':raw' ) ;
my $utf_text = read_file( $bin_file, binmode => ':utf8' ) ;
The next argument(s) is either a hash reference or a flattened hash,
C<< key => value >> pairs. The options are passed through to the
L<File::Slurp/"write_file"> function. All options are described there.
Only the C<binmode> and C<err_mode> options are supported. The call to
L<File::Slurp/"write_file"> has the C<atomic> option set so you will always
have a consistent file.
=head3 array_ref
=head2 edit_file_lines
If this boolean option is set, the return value (only in scalar
context) will be an array reference which contains the lines of the
slurped file. The following two calls are equivalent:
use File::Slurp qw(edit_file_lines);
# perl -pi -e '$_ = "" if /foo/' /path/file
edit_file_lines { $_ = '' if /foo/ } '/path/file';
edit_file_lines sub { $_ = '' if /foo/ }, '/path/file';
sub delete_foo { $_ = '' if /foo/ }
edit_file \&delete_foo, '/path/file';
my $lines_ref = read_file( $bin_file, array_ref => 1 ) ;
my $lines_ref = [ read_file( $bin_file ) ] ;
The C<edit_file_lines> function reads each line of a file into C<$_>, and
executes a code block that should modify C<$_>. It will then write C<$_> back
to the file. It is equivalent to the C<-pi> command line options of Perl but
you can call it from inside your program and not have to fork out a process.
=head3 chomp
The first argument to C<edit_file_lines> is a code block or a code reference.
The code block is not followed by a comma (as with C<grep> and C<map>) but a
code reference is followed by a comma.
If this boolean option is set, the lines are chomped. This only
happens if you are slurping in a list context or using the
C<array_ref> option.
The next argument is the filename.
=head3 scalar_ref
The next argument(s) is either a hash reference or a flattened hash,
C<< key => value >> pairs. The options are passed through to the
L<File::Slurp/"write_file"> function. All options are described there.
Only the C<binmode> and C<err_mode> options are supported. The call to
L<File::Slurp/"write_file"> has the C<atomic> option set so you will always
have a consistent file.
If this boolean option is set, the return value (only in scalar
context) will be an scalar reference to a string which is the contents
of the slurped file. This will usually be faster than returning the
plain scalar. It will also save memory as it will not make a copy of
the file to return. Run the extras/slurp_bench.pl script to see speed
comparisons.
=head2 ef
my $text_ref = read_file( $bin_file, scalar_ref => 1 ) ;
use File::Slurp qw(ef);
# perl -0777 -pi -e 's/foo/bar/g' /path/file
ef { s/foo/bar/g } '/path/file';
ef sub { s/foo/bar/g }, '/path/file';
sub replace_foo { s/foo/bar/g }
ef \&replace_foo, '/path/file';
=head3 buf_ref
The C<ef> function is simply a synonym for the L<File::Slurp/"edit_file">
function.
You can use this option to pass in a scalar reference and the slurped
file contents will be stored in the scalar. This can be used in
conjunction with any of the other options. This saves an extra copy of
the slurped file and can lower ram usage vs returning the file. It is
usually the fastest way to read a file into a scalar. Run the
extras/slurp_bench.pl script to see speed comparisons.
=head2 efl
use File::Slurp qw(efl);
# perl -pi -e '$_ = "" if /foo/' /path/file
efl { $_ = '' if /foo/ } '/path/file';
efl sub { $_ = '' if /foo/ }, '/path/file';
sub delete_foo { $_ = '' if /foo/ }
efl \&delete_foo, '/path/file';
read_file( $bin_file, buf_ref => \$buffer ) ;
The C<efl> function is simply a synonym for the L<File::Slurp/"edit_file_lines">
function.
=head3 blk_size
=head2 overwrite_file
You can use this option to set the block size used when slurping from
an already open handle (like \*STDIN). It defaults to 1MB.
use File::Slurp qw(overwrite_file);
my $res = overwrite_file('/path/file', "Some text");
my $text_ref = read_file( $bin_file, blk_size => 10_000_000,
array_ref => 1 ) ;
The C<overwrite_file> function is simply a synonym for the
L<File::Slurp/"write_file"> function.
=head3 err_mode
=head2 prepend_file
You can use this option to control how read_file behaves when an error
occurs. This option defaults to 'croak'. You can set it to 'carp' or to
'quiet to have no special error handling. This code wants to carp and
then read another file if it fails.
use File::Slurp qw(prepend_file);
prepend_file('/path/file', $header);
prepend_file('/path/file', \@lines);
prepend_file('/path/file', { binmode => ':raw'}, $bin_data);
my $text_ref = read_file( $file, err_mode => 'carp' ) ;
unless ( $text_ref ) {
# equivalent to:
use File::Slurp qw(read_file write_file);
my $content = read_file('/path/file');
my $new_content = "hahahaha";
write_file('/path/file', $new_content . $content);
# read a different file but croak if not found
$text_ref = read_file( $another_file ) ;
}
The C<prepend_file> function is the opposite of L<File::Slurp/"append_file"> as
it writes new contents to the beginning of the file instead of the end. It is a
combination of L<File::Slurp/"read_file"> and L<File::Slurp/"write_file">. It
works by first using C<read_file> to slurp in the file and then calling
C<write_file> with the new data and the existing file data.
# process ${$text_ref}
The first argument to C<prepend_file> is the filename.
=head2 B<write_file>
The next argument(s) is either a hash reference or a flattened hash,
C<< key => value >> pairs. The options are passed through to the
L<File::Slurp/"write_file"> function. All options are described there.
This sub writes out an entire file in one call.
Only the C<binmode> and C<err_mode> options are supported. The
C<write_file> call has the C<atomic> option set so you will always have
a consistent file.
write_file( 'filename', @data ) ;
=head2 read_dir
The first argument to C<write_file> is the filename. The next argument
is an optional hash reference and it contains key/values that can
modify the behavior of C<write_file>. The rest of the argument list is
the data to be written to the file.
use File::Slurp qw(read_dir);
my @files = read_dir('/path/to/dir');
# all files, even the dots
my @files = read_dir('/path/to/dir', keep_dot_dot => 1);
# keep the full file path
my @paths = read_dir('/path/to/dir', prefix => 1);
# scalar context
my $files_ref = read_dir('/path/to/dir');
write_file( 'filename', {append => 1 }, @data ) ;
write_file( 'filename', {binmode => ':raw'}, $buffer ) ;
This function returns a list of the filenames in the supplied directory. In
list context, an array is returned, in scalar context, an array reference is
returned.
As a shortcut if the first data argument is a scalar or array reference,
it is used as the only data to be written to the file. Any following
arguments in @_ are ignored. This is a faster way to pass in the output
to be written to the file and is equivalent to the C<buf_ref> option of
C<read_file>. These following pairs are equivalent but the pass by
reference call will be faster in most cases (especially with larger
files).
The first argument is the path to the directory to read.
write_file( 'filename', \$buffer ) ;
write_file( 'filename', $buffer ) ;
The next argument(s) is either a hash reference or a flattened hash,
C<< key => value >> pairs. The following options are available:
write_file( 'filename', \@lines ) ;
write_file( 'filename', @lines ) ;
=over
If the first argument is a handle (if it is a ref and is an IO or GLOB
object), then that handle is written to. This mode is supported so you
spew to handles such as \*STDOUT. See the test handle.t for an example
that does C<open( '-|' )> and child process spews data to the parent
which slurps it in. All of the options that control how the data are
passed into C<write_file> still work in this case.
=item
If the first argument is an overloaded object then its stringified value
is used for the filename and that file is opened. This is new feature
in 9999.14. See the stringify.t test for an example.
err_mode
By default C<write_file> returns 1 upon successfully writing the file or
undef if it encountered an error. You can change how errors are handled
with the C<err_mode> option.
The C<err_mode> option has three possible values: C<quiet>, C<carp>, or the
default, C<croak>. In C<quiet> mode, all errors will be silent. In C<carp> mode,
all errors will be emitted as warnings. And, in C<croak> mode, all errors will
be emitted as exceptions. Take a look at L<Try::Tiny> or
L<Syntax::Keyword::Try> to see how to catch exceptions.
The options are:
=item
=head3 binmode
keep_dot_dot
If you set the binmode option, then its value is passed to a call to
binmode on the opened handle. You can use this to set the file to be
read in binary mode, utf8, etc. See perldoc -f binmode for more.
The C<keep_dot_dot> option is a boolean option, defaulted to false (C<0>).
Setting this option to true (C<1>) will also return the C<.> and C<..> files
that are removed from the file list by default.
write_file( $bin_file, {binmode => ':raw'}, @data ) ;
write_file( $bin_file, {binmode => ':utf8'}, $utf_text ) ;
=item
=head3 perms
prefix
The perms option sets the permissions of newly-created files. This value
is modified by your process's umask and defaults to 0666 (same as
sysopen).
The C<prefix> option is a boolean option, defaulted to false (C<0>).
Setting this option to true (C<1>) add the directory as a prefix to the file.
The directory and the filename are joined using C<< File::Spec->catfile() >> to
ensure the proper directory separator is used for your OS. See L<File::Spec>.
NOTE: this option is new as of File::Slurp version 9999.14;
=back
=head3 buf_ref
=head2 read_file
You can use this option to pass in a scalar reference which has the
data to be written. If this is set then any data arguments (including
the scalar reference shortcut) in @_ will be ignored. These are
equivalent:
use File::Slurp qw(read_file);
my $text = read_file('/path/file');
my $bin = read_file('/path/file', { binmode => ':raw' });
my @lines = read_file('/path/file');
my $lines_ref = read_file('/path/file', array_ref => 1);
my $lines_ref = [ read_file('/path/file') ];
write_file( $bin_file, { buf_ref => \$buffer } ) ;
write_file( $bin_file, \$buffer ) ;
write_file( $bin_file, $buffer ) ;
# or we can read into a buffer:
my $buffer;
read_file('/path/file', buf_ref => \$buffer);
=head3 atomic
# or we can set the block size for the read
my $text_ref = read_file('/path/file', blk_size => 10_000_000, array_ref => 1);
If you set this boolean option, the file will be written to in an
atomic fashion. A temporary file name is created by appending the pid
($$) to the file name argument and that file is spewed to. After the
file is closed it is renamed to the original file name (and rename is
an atomic operation on most OS's). If the program using this were to
crash in the middle of this, then the file with the pid suffix could
be left behind.
# or we can get a scalar reference
my $text_ref = read_file('/path/file', scalar_ref => 1);
=head3 append
This function reads in an entire file and returns its contents to the
caller. In scalar context it returns the entire file as a single
scalar. In list context it will return a list of lines (using the
current value of C<$/> as the separator, including support for paragraph
mode when it is set to C<''>).
If you set this boolean option, the data will be written at the end of
the current file. Internally this sets the sysopen mode flag O_APPEND.
The first argument is the path to the file to be slurped in.
write_file( $file, {append => 1}, @data ) ;
The next argument(s) is either a hash reference or a flattened hash,
C<< key => value >> pairs. The following options are available:
You
can import append_file and it does the same thing.
=over
=head3 no_clobber
=item
If you set this boolean option, an existing file will not be overwritten.
array_ref
write_file( $file, {no_clobber => 1}, @data ) ;
The C<array_ref> option is a boolean option, defaulted to false (C<0>). Setting
this option to true (C<1>) will only have relevance if the C<read_file> function
is called in scalar context. When true, the C<read_file> function will return
a reference to an array of the lines in the file.
=head3 err_mode
=item
You can use this option to control how C<write_file> behaves when an
error occurs. This option defaults to 'croak'. You can set it to
'carp' or to 'quiet' to have no error handling other than the return
value. If the first call to C<write_file> fails it will carp and then
write to another file. If the second call to C<write_file> fails, it
will croak.
binmode
unless ( write_file( $file, { err_mode => 'carp', \$data ) ;
The C<binmode> option is a string option, defaulted to empty (C<''>). If you
set the C<binmode> option, then its value is passed to a call to C<binmode> on
the opened handle. You can use this to set the file to be read in binary mode,
utf8, etc. See C<perldoc -f binmode> for more.
# write a different file but croak if not found
write_file( $other_file, \$data ) ;
}
=item
=head2 overwrite_file
blk_size
This sub is just a typeglob alias to write_file since write_file
always overwrites an existing file. This sub is supported for
backwards compatibility with the original version of this module. See
write_file for its API and behavior.
You can use this option to set the block size used when slurping from
an already open handle (like C<\*STDIN>). It defaults to 1MB.
=head2 append_file
=item
This sub will write its data to the end of the file. It is a wrapper
around write_file and it has the same API so see that for the full
documentation. These calls are equivalent:
buf_ref
append_file( $file, @data ) ;
write_file( $file, {append => 1}, @data ) ;
The C<buf_ref> option can be used in conjunction with any of the other options.
You can use this option to pass in a scalar reference and the slurped
file contents will be stored in the scalar. This saves an extra copy of
the slurped file and can lower RAM usage vs returning the file. It is
usually the fastest way to read a file into a scalar.
=item
=head2 prepend_file
chomp
This sub writes data to the beginning of a file. The previously existing
data is written after that so the effect is prepending data in front of
a file. It is a counterpart to the append_file sub in this module. It
works by first using C<read_file> to slurp in the file and then calling
C<write_file> with the new data and the existing file data.
The C<chomp> option is a boolean option, defaulted to false (C<0>). Setting
this option to true (C<1>) will cause each line to have its contents C<chomp>ed.
This option works in list context or in scalar context with the C<array_ref>
option.
The first argument to C<prepend_file> is the filename. The next argument
is an optional hash reference and it contains key/values that can modify
the behavior of C<prepend_file>. The rest of the argument list is the
data to be written to the file and that is passed to C<write_file> as is
(see that for allowed data).
=item
Only the C<binmode> and C<err_mode> options are supported. The
C<write_file> call has the C<atomic> option set so you will always have
a consistant file. See above for more about those options.
err_mode
C<prepend_file> is not exported by default, you need to import it
explicitly.
The C<err_mode> option has three possible values: C<quiet>, C<carp>, or the
default, C<croak>. In C<quiet> mode, all errors will be silent. In C<carp> mode,
all errors will be emitted as warnings. And, in C<croak> mode, all errors will
be emitted as exceptions. Take a look at L<Try::Tiny> or
L<Syntax::Keyword::Try> to see how to catch exceptions.
use File::Slurp qw( prepend_file ) ;
prepend_file( $file, $header ) ;
prepend_file( $file, \@lines ) ;
prepend_file( $file, { binmode => 'raw:'}, $bin_data ) ;
=item
scalar_ref
=head2 edit_file, edit_file_lines
The C<scalar_ref> option is a boolean option, defaulted to false (C<0>). It only
has meaning in scalar context. The return value will be a scalar reference to a
string which is the contents of the slurped file. This will usually be faster
than returning the plain scalar. It will also save memory as it will not make a
copy of the file to return.
These subs read in a file into $_, execute a code block which should
modify $_ and then write $_ back to the file. The difference between
them is that C<edit_file> reads the whole file into $_ and calls the
code block one time. With C<edit_file_lines> each line is read into $_
and the code is called for each line. In both cases the code should
modify $_ if desired and it will be written back out. These subs are
the equivalent of the -pi command line options of Perl but you can
call them from inside your program and not fork out a process. They
are in @EXPORT_OK so you need to request them to be imported on the
use line or you can import both of them with:
=back
use File::Slurp qw( :edit ) ;
=head2 rf
The first argument to C<edit_file> and C<edit_file_lines> is a code
block or a code reference. The code block is not followed by a comma
(as with grep and map) but a code reference is followed by a
comma. See the examples below for both styles. The next argument is
the filename. The last argument is an optional hash reference and it
contains key/values that can modify the behavior of
C<prepend_file>.
use File::Slurp qw(rf);
my $text = rf('/path/file');
Only the C<binmode> and C<err_mode> options are supported. The
C<write_file> call has the C<atomic> option set so you will always
have a consistant file. See above for more about those options.
The C<rf> function is simply a synonym for the L<File::Slurp/"read_file">
function.
Each group of calls below show a Perl command line instance and the
equivalent calls to C<edit_file> and C<edit_file_lines>.
=head2 slurp
perl -0777 -pi -e 's/foo/bar/g' filename
use File::Slurp qw( edit_file ) ;
edit_file { s/foo/bar/g } 'filename' ;
edit_file sub { s/foo/bar/g }, 'filename' ;
edit_file \&replace_foo, 'filename' ;
sub replace_foo { s/foo/bar/g }
use File::Slurp qw(slurp);
my $text = slurp('/path/file');
perl -pi -e '$_ = "" if /foo/' filename
use File::Slurp qw( edit_file_lines ) ;
use File::Slurp ;
edit_file_lines { $_ = '' if /foo/ } 'filename' ;
edit_file_lines sub { $_ = '' if /foo/ }, 'filename' ;
edit_file \&delete_foo, 'filename' ;
sub delete_foo { $_ = '' if /foo/ }
The C<slurp> function is simply a synonym for the L<File::Slurp/"read_file">
function.
=head2 read_dir
=head2 wf
use File::Slurp qw(wf);
my $res = wf('/path/file', "Some text");
The C<wf> function is simply a synonym for the
L<File::Slurp/"write_file"> function.
=head2 write_file
use File::Slurp qw(write_file);
write_file('/path/file', @data);
write_file('/path/file', {append => 1}, @data);
write_file('/path/file', {binmode => ':raw'}, $buffer);
write_file('/path/file', \$buffer);
write_file('/path/file', $buffer);
write_file('/path/file', \@lines);
write_file('/path/file', @lines);
This sub reads all the file names from directory and returns them to
the caller but C<.> and C<..> are removed by default.
# binmode
write_file('/path/file', {binmode => ':raw'}, @data);
write_file('/path/file', {binmode => ':utf8'}, $utf_text);
my @files = read_dir( '/path/to/dir' ) ;
# buffered
write_file('/path/file', {buf_ref => \$buffer});
write_file('/path/file', \$buffer);
write_file('/path/file', $buffer);
The first argument is the path to the directory to read. If the next
argument is a hash reference, then it is used as the options.
Otherwise the rest of the argument list are is used as key/value
options.
# append
write_file('/path/file', {append => 1}, @data);
In list context C<read_dir> returns a list of the entries in the
directory. In a scalar context it returns an array reference which has
the entries.
# no clobbering
write_file('/path/file', {no_clobber => 1}, @data);
=head3 err_mode
This function writes out an entire file in one call. By default C<write_file>
returns C<1> upon successfully writing the file or C<undef> if it encountered
an error. You can change how errors are handled with the C<err_mode> option.
If the C<err_mode> option is set, it selects how errors are handled (see
C<err_mode> in C<read_file> or C<write_file>).
The first argument to C<write_file> is the filename.
=head3 keep_dot_dot
The next argument(s) is either a hash reference or a flattened hash,
C<< key => value >> pairs. The following options are available:
If this boolean option is set, C<.> and C<..> are not removed from the
list of files.
=over
my @all_files = read_dir( '/path/to/dir', keep_dot_dot => 1 ) ;
=item
=head3 prefix
append
If this boolean option is set, the string "$dir/" is prefixed to each
dir entry. This means you can directly use the results to open
files. A common newbie mistake is not putting the directory in front
of entries when opening themn.
The C<append> option is a boolean option, defaulted to false (C<0>). Setting
this option to true (C<1>) will cause the data to be be written at the end of
the current file. Internally this sets the C<sysopen> mode flag C<O_APPEND>.
my @paths = read_dir( '/path/to/dir', prefix => 1 ) ;
The L<File::Slurp/"append_file"> function sets this option by default.
=head2 EXPORT
=item
These are exported by default or with
use File::Slurp qw( :std ) ;
atomic
read_file write_file overwrite_file append_file read_dir
The C<atomic> option is a boolean option, defaulted to false (C<0>). Setting
this option to true (C<1>) will cause the file to be be written to in an
atomic fashion. A temporary file name is created using L<File::Temp/"tempfile">.
After the file is closed it is renamed to the original file name
(and C<rename> is an atomic operation on most OSes). If the program using
this were to crash in the middle of this, then the temporary file could
be left behind.
=item
binmode
The C<binmode> option is a string option, defaulted to empty (C<''>). If you
set the C<binmode> option, then its value is passed to a call to C<binmode> on
the opened handle. You can use this to set the file to be read in binary mode,
utf8, etc. See C<perldoc -f binmode> for more.
=item
buf_ref
The C<buf_ref> option is used to pass in a scalar reference which has the
data to be written. If this is set then any data arguments (including
the scalar reference shortcut) in C<@_> will be ignored.
=item
err_mode
The C<err_mode> option has three possible values: C<quiet>, C<carp>, or the
default, C<croak>. In C<quiet> mode, all errors will be silent. In C<carp> mode,
all errors will be emitted as warnings. And, in C<croak> mode, all errors will
be emitted as exceptions. Take a look at L<Try::Tiny> or
L<Syntax::Keyword::Try> to see how to catch exceptions.
=item
no_clobber
The C<no_clobber> option is a boolean option, defaulted to false (C<0>). Setting
this option to true (C<1>) will ensure an that existing file will not be
overwritten.
These are exported with
use File::Slurp qw( :edit ) ;
=item
edit_file edit_file_lines
perms
You can get all subs in the module exported with
use File::Slurp qw( :all ) ;
The C<perms> option sets the permissions of newly-created files. This value
is modified by your process's C<umask> and defaults to C<0666> (same as
C<sysopen>).
=head2 LICENSE
NOTE: this option is new as of File::Slurp version 9999.14.
Same as Perl.
=back
=head2 SEE ALSO
=head1 EXPORT
An article on file slurping in extras/slurp_article.pod. There is
also a benchmarking script in extras/slurp_bench.pl.
These are exported by default or with
=head2 BUGS
use File::Slurp qw(:std);
# read_file write_file overwrite_file append_file read_dir
If run under Perl 5.004, slurping from the DATA handle will fail as
that requires B.pm which didn't get into core until 5.005.
These are exported with
use File::Slurp qw(:edit);
# edit_file edit_file_lines
You can get all subs in the module exported with
use File::Slurp qw(:all);
=head1 SEE ALSO
=over
=item *
L<File::Slurper> - Provides a straightforward set of functions for the most
common tasks of reading/writing text and binary files.
=item *
L<Path::Tiny> - Lightweight and comprehensive file handling, including simple
methods for reading, writing, and editing text and binary files.
=item *
L<Mojo::File> - Similar to Path::Tiny for the L<Mojo> toolkit, always works in
bytes.
=back
=head1 AUTHOR
Uri Guttman, E<lt>uri AT stemsystems DOT comE<gt>
Uri Guttman, <F<uri@stemsystems.com>>
=head1 COPYRIGHT & LICENSE
Copyright (c) 2003 Uri Guttman. All rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment