Sidef

 view release on metacpan or  search on metacpan

lib/Sidef/Types/Glob/File.pod  view on Meta::CPAN


Returns the number of days since the file was last accessed, relative to the current time. The access time (atime) is updated when the file is read.

    var file = File("log.txt")
    say "Last accessed #{file.access_time_days_diff} days ago"

=cut

=head2 append

    file.append(string, mode=':utf8')

Appends the given string to the file. If the file doesn't exist, it will be created. The optional C<mode> parameter specifies the encoding layer (default is UTF-8).

    var file = File("log.txt")
    file.append("New log entry\n")
    file.append("Binary data", ':raw')

Returns the File object for method chaining.

=cut

=head2 base

    file.base

Returns the basename of the file (the filename without the directory path), similar to the Unix shell command C<basename>.

    var file = File("/home/user/document.txt")
    say file.base                   # document.txt

Aliases: I<base_name>, I<basename>

=cut

=head2 change_time_days_diff

    file.change_time_days_diff

Returns the number of days since the file's inode change time (ctime), relative to the current time. The change time is updated when file metadata (permissions, ownership, etc.) or content changes.

    var file = File("config.ini")
    say "Last changed #{file.change_time_days_diff} days ago"

=cut

=head2 chmod

    file.chmod(permission)

Changes the file's permission bits. The permission can be specified as an octal number or a string.

    var file = File("script.sh")
    file.chmod(0755)                # rwxr-xr-x
    file.chmod("644")               # rw-r--r--

Returns C<true> on success, C<false> otherwise.

=cut

=head2 chown

    file.chown(uid, gid)

Changes the owner and group of the file. Both C<uid> (user ID) and C<gid> (group ID) must be provided as numeric values.

    var file = File("data.txt")
    file.chown(1000, 1000)          # Change owner and group

Returns C<true> on success, C<false> otherwise. May require elevated privileges.

=cut

=head2 compare

    file.compare(other_file)

Compares the contents of two files byte-by-byte. Returns C<0> if files are identical, C<-1> if the first file is less than the second, C<1> if greater, or C<nil> on error.

    var file1 = File("original.txt")
    var file2 = File("copy.txt")
    
    given (file1.compare(file2)) {
        when (0)  { say "Files are identical" }
        when (-1) { say "file1 < file2" }
        when (1)  { say "file1 > file2" }
        default   { say "Error comparing files" }
    }

=cut

=head2 cp

    file.cp(destination)

Copies the file to the specified destination. The destination can be either a filename or a File object. If the destination is a directory, the file is copied into that directory with the same basename.

    var file = File("source.txt")
    file.cp("backup.txt")           # Copy to backup.txt
    file.cp(Dir("backups"))         # Copy into backups directory

Returns C<true> on success, C<false> otherwise.

Aliases: I<copy>

=cut

=head2 delete

    file.delete

Deletes the file from the filesystem and returns C<true> on success, C<false> otherwise. This method will not attempt to delete directories; use C<Dir.remove> for directories instead.

    var file = File("temporary.txt")
    file.delete && say "File deleted successfully"

Aliases: I<remove>

=cut

=head2 dir

    file.dir

Returns the directory portion of the filename as a String, similar to the Unix shell command C<dirname>. This is the parent directory containing the file.

    var file = File("/home/user/documents/file.txt")
    say file.dir                    # /home/user/documents



( run in 0.418 second using v1.01-cache-2.11-cpan-5511b514fd6 )