App-DistSync
view release on metacpan or search on metacpan
bin/distsync view on Meta::CPAN
use Getopt::Long;
use Pod::Usage;
use Cwd qw/getcwd/;
use File::Spec;
use App::DistSync;
use App::DistSync::LockFile;
use App::DistSync::Util;
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';
use constant {
PIDFILE => 'distsync.pid',
CMDDEFAULT => 'sync',
COMMANDS => [qw/init status sync manifest mkmani/],
};
$| = 1; # Autoflush on
lib/App/DistSync/Util.pm view on Meta::CPAN
my $yaml = read_yaml($yaml_file);
Read YAML file
=head2 slurp
my $data = slurp($file, %args);
my $data = slurp($file, { %args });
slurp($file, { buffer => \my $data });
my $data = slurp($file, { binmode => ":raw:utf8" });
Reads file $filename into a scalar
my $data = slurp($file, { binmode => ":unix" });
Reads file in fast, unbuffered, raw mode
my $data = slurp($file, { binmode => ":unix:encoding(UTF-8)" });
Reads file with UTF-8 encoding
By default it returns this scalar. Can optionally take these named arguments:
=over 4
=item binmode
Set the layers to read the file with. The default will be something sensible on your platform
=item block_size
Set the buffered block size in bytes, default to 1048576 bytes (1 MiB)
=item buffer
Pass a reference to a scalar to read the file into, instead of returning it by value.
lib/App/DistSync/Util.pm view on Meta::CPAN
=back
See also L</spew> to writing data to file
=head2 spew
spew($file, $data, %args);
spew($file, $data, { %args });
spew($file, \$data, { %args });
spew($file, \@data, { %args });
spew($file, $data, { binmode => ":raw:utf8" });
Writes data to a file atomically. The only argument is C<binmode>, which is passed to
C<binmode()> on the handle used for writing.
Can optionally take these named arguments:
=over 4
=item append
This argument is a boolean option, defaulted to false (C<0>).
Setting this argument to true (C<1>) will cause the data to be be written at the end of the current file.
Internally this sets the sysopen mode flag C<O_APPEND>
=item binmode
Set the layers to write the file with. The default will be something sensible on your platform
=item locked
This argument is a boolean option, defaulted to false (C<0>).
Setting this argument to true (C<1>) will ensure an that existing file will not be overwritten
=item mode
lib/App/DistSync/Util.pm view on Meta::CPAN
if (ref($file)) {
$fh = $file;
$cleanup = 0; # Disable closing filehandle for passed filehandle
} else {
$fh = IO::File->new($file, "r");
unless (defined $fh) {
carp qq/Can't open file "$file": $!/;
return;
}
}
# Set binmode layer
my $bm = $args->{binmode} // ':raw'; # read in :raw by default
$fh->binmode($bm);
# Set buffer
my $buf;
my $buf_ref = $args->{buffer} // \$buf;
${$buf_ref} = ''; # Set empty string to buffer
my $blk_size = $args->{block_size} || 1024 * 1024; # Set block size (1 MiB)
# Read whole file
my ($pos, $ret) = (0, 0);
while ($ret = $fh->read(${$buf_ref}, $blk_size, $pos)) {
$pos += $ret if defined $ret;
}
lib/App/DistSync/Util.pm view on Meta::CPAN
$fh->close if $cleanup; # automatically closes the file
# Return content if no buffer specified
return if defined $args->{buffer};
return ${$buf_ref};
}
sub spew {
my $file = shift // '';
my $data = shift // '';
my $args = @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {};
my $cleanup = 1;
# Get binmode layer, mode and perms
my $bm = $args->{binmode} // ':raw'; # read in :raw by default
my $perms = $args->{perms} // 0666; # set file permissions
my $mode = $args->{mode} // O_WRONLY | O_CREAT;
$mode |= O_APPEND if $args->{append};
$mode |= O_EXCL if $args->{locked};
# Open filehandle
my $fh;
if (ref($file)) {
$fh = $file;
$cleanup = 0; # Disable closing filehandle for passed filehandle
} else {
$fh = IO::File->new($file, $mode, $perms);
unless (defined $fh) {
carp qq/Can't open file "$file": $!/;
return;
}
}
# Set binmode layer
$fh->binmode($bm);
# Set buffer
my $buf;
my $buf_ref = \$buf;
if (ref($data) eq 'SCALAR') {
$buf_ref = $data;
} elsif (ref($data) eq 'ARRAY') {
${$buf_ref} = join '', @$data;
} else {
$buf_ref = \$data;
}
( run in 1.438 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )