File-DataClass
view release on metacpan or search on metacpan
lib/File/DataClass/IO.pm view on Meta::CPAN
$self->$_push_layer( $layer )
and $self->is_open and $self->$_sane_binmode( $layer );
return $self;
}
sub block_size {
defined $_[ 1 ] and $_[ 0 ]->_block_size( $_[ 1 ] ); return $_[ 0 ];
}
sub buffer {
my $self = shift;
if (@_) {
my $buffer_ref = ref $_[ 0 ] ? $_[ 0 ] : \$_[ 0 ];
defined ${ $buffer_ref } or ${ $buffer_ref } = NUL;
$self->{buffer} = $buffer_ref;
return $self;
}
exists $self->{buffer} or $self->{buffer} = do { my $x = NUL; \$x };
return $self->{buffer};
}
sub canonpath {
return File::Spec->canonpath( $_[ 0 ]->name );
}
sub catdir {
my ($self, @args) = @_; return $self->child( @args )->dir;
}
sub catfile {
my ($self, @args) = @_; return $self->child( @args )->file;
}
sub child {
my ($self, @args) = @_;
my $params = (is_hashref $args[ -1 ]) ? pop @args : {};
my $args = [ grep { defined and CORE::length } $self->name, @args ];
return $self->$_constructor( $args, $params );
}
sub chmod {
my ($self, $perms) = @_;
$perms //= $self->_perms; # uncoverable condition false
CORE::chmod $perms, $self->name;
return $self;
}
sub chomp {
$_[ 0 ]->_chomp( TRUE ); return $_[ 0 ];
}
sub chown {
my ($self, $uid, $gid) = @_;
(defined $uid and defined $gid)
or $self->$_throw( Unspecified, [ 'user or group id' ] );
1 == CORE::chown $uid, $gid, $self->name
or $self->$_throw( 'Path [_1 chown failed to [_2]/[_3]',
[ $self->name, $uid, $gid ] );
return $self;
}
sub clear {
${ $_[ 0 ]->buffer } = NUL; return $_[ 0 ];
}
sub close {
my $self = shift; $self->is_open or return $self;
if (is_ntfs) { # uncoverable branch true
$self->$_close_and_rename; # uncoverable statement
} else { $self->$_rename_and_close }
$self->_set_io_handle( undef );
$self->_set_is_open ( FALSE );
$self->_set_mode ( 'r' );
return $self;
}
sub copy {
my ($self, $to) = @_;
$to or $self->$_throw( Unspecified, [ 'copy to' ] );
(blessed $to and $to->isa( __PACKAGE__ ))
or $to = $self->$_constructor( $to );
File::Copy::copy( $self->name, $to->pathname )
or $self->$_throw( 'Cannot copy [_1] to [_2]',
[ $self->name, $to->pathname ] );
return $to;
}
sub cwd {
my $self = shift; return $self->$_constructor( Cwd::getcwd(), @_ );
}
sub deep {
$_[ 0 ]->_deep( TRUE ); return $_[ 0 ];
}
sub delete {
my $self = shift; my $path = $self->$_get_atomic_path;
$self->_atomic and -f $path and unlink $path;
return $self->close;
}
sub delete_tmp_files {
my ($self, $tmplt) = @_; $tmplt //= '%6.6d....';
my $pat = sprintf $tmplt, $PID;
while (my $entry = $self->next) {
$entry->filename =~ m{ \A $pat \z }mx and unlink $entry->pathname;
}
lib/File/DataClass/IO.pm view on Meta::CPAN
=head2 binmode
$io = io( 'path_to_file' )->binmode( $layer );
Sets binmode to the given layer
=head2 block_size
$io = io( 'path_to_file' )->block_size( 1024 );
Defaults to 1024. The default block size used by the L</read> method
=head2 buffer
The internal buffer used by L</read> and L</write>
=head2 _build__dir_pattern
Returns the pattern that will match against the current or parent
directory
=head2 canonpath
$canonpath = io( '././path_to_file' )->canonpath;
Returns the canonical path for the object
=head2 catdir
$io = io( 'path_to_directory' )->catdir( 'additional_directory_path' );
Create a new IO directory object by concatenating this objects pathname
with the one that is supplied
=head2 catfile
$io = io( 'path_to_directory' )->catfile( 'additional_file_path' );
Create a new IO file object by concatenating this objects pathname
with the one that is supplied
=head2 child
$io = io( 'path_to_directory' )->child( 'additional_file_path' );
Like L</catdir> and L</catfile> but does not set the object type
=head2 chmod
$io = io( 'path_to_file' )->chmod( '0644' );
Changes the permission on the file to the selected value. Permission values
can be either octal or string
=head2 chomp
$io = io( 'path_to_file' )->chomp;
Causes input lines to be chomped when L</getline> or L</getlines> are called
=head2 chown
$io = io( 'path_to_file' )->chown( $uid, $gid );
Changes user and group ownership
=head2 clear
$io->clear
Set the contents of the internal buffer to the null string
=head2 close
$io->close;
Close the file or directory handle depending on type
If the temporary atomic file exists, renames it to the original
filename. Unlocks the file if it was locked. Closes the file handle
=head2 copy
$dest_obj = io( 'path_to_file' )->copy( $destination_path_or_object );
Copies the file to the destination. The destination can be either a path or
and IO object. Returns the destination object
=head2 cwd
$current_working_directory = io()->cwd;
Returns the current working directory wrapped in a L<File::DataClass::IO>
object
=head2 deep
@files = io( 'path_to_root_of_tree' )->deep->all_files
Changes the default level for the L</all> methods to zero so
that the whole directory tree is searched
=head2 delete
Deletes the atomic update temporary file if it exists. Then calls
L</close>
=head2 delete_tmp_files
$io = io( $tempdir )->delete_tmp_files( $template );
Delete temporary files for this process (temporary file names include
the process id). Temporary files are stored in the C<$tempdir>. Can override
the template filename pattern if required
=head2 digest
$digest_object = io( 'path_to_file' )->digest( $algorithm, $options );
Returns a L<Digest> object which is calculated from the contents of the
specified file. The arguments are optional. The algorithm defaults to
C<SHA-256>. The C<$options> hash reference takes the C<block_size> parameter
which causes the file to read through the buffer C<block_size> bytes at a
( run in 1.694 second using v1.01-cache-2.11-cpan-71847e10f99 )