Acrux
view release on metacpan or search on metacpan
lib/Acrux/Digest.pm view on Meta::CPAN
return $self;
}
sub addfile {
my $self = shift;
my $fh = shift;
return $self unless $fh;
if (!ref($fh) && ref(\$fh) ne "GLOB") {
$fh = IO::File->new($fh, "r");
return $self unless $fh;
}
$fh->binmode() or croak(sprintf("Can't switch to binmode: %s", $!));
my $buf;
while ($fh->read($buf, BUFFER_SIZE)) {
$self->add($buf);
}
$fh->close() or croak(sprintf("Can't close file: %s", $!));
return $self;
}
sub reset {
my $self = shift;
$self->{data} = "";
lib/Acrux/Util.pm view on Meta::CPAN
For example:
$rand = randchars( 8, [qw/a b c d e f/]); # -> cdeccfdf
=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/Acrux/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/Acrux/Util.pm view on Meta::CPAN
$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)) {
lib/Acrux/Util.pm view on Meta::CPAN
# 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 0.712 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )