NAME

File::Operations - file system specific functions


SYNOPSIS

  use File::Operations;
  use Exception;
  try {
    make_path("/tmp/my-own/dir");
    my $file_content = load_file("/etc/issue");
    save_file("/tmp/my-own/dir/issue", \$file_content);
    # This is equivalent to the previous two lines, but optimized
    copy_file("/etc/issue", "/tmp/my-own/dir/issue");
    make_dir("/tmp/my-own/dir2", 0711);
    copy_file("/etc/issue", "/tmp/my-own/dir2/issue");
    copy_file("/tmp/my-own/dir2/issue", "/tmp/my-own/dir2/issue2");
    remove_file("/tmp/my-own/dir2/issue2");
    clean_dir("/tmp/my-own/dir2"); # no effect, it's empty already
    remove_dir("/tmp/my-own");
  } catch('Exception::File::Operations'), sub {
    print "File System Error: ", $_->get_message, "\n";
  };

or just (lets exception throw):

  copy_file("origin.txt", "backup.txt");

Optionally, a debbuging can be turned on:

  use Pragma::DebugOutput qw(File::Operations);


DESCRIPTION

This package contains common low-level file operation functions.

All functions throw Exception::File::Operations, instead of returning undef, like in older versions. However, on unfatal errors, exception is not thrown and undef is returned.


REQUIREMENTS

the Cwd manpage, the File::Basename manpage, the File::Copy manpage, the Encode manpage, the Exception manpage, the Debug manpage.


METHODS

load_file

usage
  $content = load_file($filename)
description
Loads file content with given file-name on the local filesystem.

If filename is ``-'' then the standart input is used (in this case the optional ``is_utf8'' parameter has no effect, use binmode on STDIN instead).

parameters
  * filename - name of the file to be loaded
  * optional params hash with keys:
      content_ref - scalarref or arrayref to store the content into
      is_utf8 - whether content is utf8 (by default 0)
returns
File content (string) on success, otherwise throws exception.

save_file

description
Saves content to a file on the local filesystem with give filename.

If filename is ``-'' then the standart output is used (in this case the optional ``is_utf8'' parameter has no effect, use binmode on STDOUT instead).

usage
  save_file($filename, $content);
parameters
  * filename - name of the file to be saved into
  * content - content to store (string, scalarref or arrayref)
  * optional params hash with keys:
      create_dirs - boolean (default is 0 - don't create subdirs)
      is_utf8 - whether content is utf8 (by default autodetected)
returns
1 on success, otherwise throws exception.

append_file

description
Appends content to a file on the local filesystem with give filename.

If filename is ``-'' then the standart output is used (in this case the optional ``is_utf8'' parameter has no effect, use binmode on STDOUT instead).

usage
  append_file($filename, $content);
parameters
  * filename - name of the file to be saved into
  * content - content to append (string, scalarref or arrayref)
  * optional params hash with keys:
      is_utf8 - whether content is utf8 (by default autodetected)
returns
1 on success, otherwise throws exception.

load_field_file

usage
  $field_table = load_field_file($filename)
description
Loads file content with given file-name on the local filesystem.

parameters
  * filename - name of the file to be loaded
  * optional param hash

Possible keys of param hash are:

  * delim (default is TAB)
  * unescape (if given, then "\t", "\n" and "\r" substrings in fields
    are interpreted as tab and end-of-lines; "\\t" as "\t" and so on)
  * num_fields (if given, the actual number of fields is verified)
  * field_names (if given, the return value is arrayref of hashrefs)
returns
The 2-dimentional arrayref (lines of fields) on success, otherwise throws exception.

save_field_file

usage
  $field_table = save_field_file($filename, $field_table)
description
Saves field_table that is 2-dimentional arrayref (lines of fields) to a file on the local filesystem with give filename.

parameters
  * filename - name of the file to be loaded
  * field_table - arrayref of arrayrefs
  * optional param hash

Possible keys of param hash are:

  * delim (default is TAB)
  * escape (if given, then tab and end-of-lines in fields are stored
    as "\t", "\n" and "\r"; "\t" as "\\t" and so on)
  * create_dirs (if true, allow creating missing sub-directories)
  * num_fields (if given, the actual number of fields is verified)
  * field_names (if given, the field_table is arrayref of hashrefs)
returns
1 on success, otherwise throws exception.

remove_file

description
Removes all files from given directory.

usage
  remove_file($filename);
parameters
 * filename - name of the file to be deleted
returns
1 on success, otherwise throws exception.

make_dir

description
Removes all files from given directory.

usage
  make_dir($PREVIEW_DIR);
parameters
  * directory to make
  * optional creating dir permissions (default is $DEFAULT_DIR_PERM)
returns
1 on success, otherwise throws exception.

make_path

description
Removes all files from given directory.

usage
  make_path($PUBLISH_DIR);
parameters
  * path to make
  * optional creating dir permissions (default is $DEFAULT_DIR_PERM)
returns
1 on success, otherwise throws exception.

copy_file

description
Copies a file to another location.

usage
  copy_file($from, $to);
parameters
  * file name to copy from
  * file name to copy to
returns
1 on success, otherwise throws exception.

move_file

description
Moves (or renames) a file to another location.

usage
  move_file($from, $to);
parameters
  * file name to move from
  * file name to move to
returns
1 on success, otherwise throws exception.

clean_dir

description
Removes all files from given directory.

usage
  clean_dir($PREVIEW_DIR);
parameters
  * directory to clean
  * optional flag:
    0 - don't go recursively, unlink files in first level only
    1 - recursively clean subdirs (default)
    2 - unlink subdirs
    3 - unlink given directory
returns
1 on success, otherwise throws exception.

remove_dir

description
Entirely removes given directory and its content (if any). This is an alias to clean_dir(3).

usage
  remove_dir($TMP_DIR);
parameters
  * directory to clean
returns
1 on success, otherwise throws exception.

copy_dir

description
Recursively copies all files and subdirectories inside given directory to new location.

Destination directory must not exist. Use: trap { remove_dir($dest); }; to remove it before copying.

usage
  copy_dir($dir_from, $dir_to);
parameters
  * source directory to copy
  * destination directory to copy to (may not exist)
  * optional creating dir permissions (default is $DEFAULT_DIR_PERM)
returns
1 on success, otherwise throws exception.

move_dir

description
Moves (or actually renames) a directory to another location.

Destination directory must not exist. Use: trap { remove_dir($dest); }; to remove it before copying.

usage
  move_dir($dir_from, $dir_to);
parameters
  * source directory to move from
  * destination directory to move to (must not exist)
returns
1 on success, otherwise throws exception.

list_filenames

description
Returns the file names in the given directory including all types of files (regular, directory, link, other), not including '.' and '..' entries.

usage
  # mini file lister
  foreach my $file (@{list_filenames('/home/ftp')}) {
    print "File $file\n" if -f $file;
    print "Dir  $file\n" if -d $file;
  }
parameters
  * directory to list
returns
Array ref of scalars (file names). Throws exception on error.

default_dir_perm

description
This functions changes default directory permissions, used in make_dir, make_path, copy_dir and move_dir functions.

The default of this package is 0775.

If no parameters specified, nothing is set (only current value is returned).

usage
  default_dir_perm(0700);
parameters
  * optional default directory permission (integer)
returns
Previous value.

preserve_stat

description
This functions changes behavior of copy_file and copy_dir functions. If 0 is given as a parameter stats will not be preserved.

TODO: specify values for diferent preserves:

   0 nothing
   1 mode   file mode  (type and permissions)
   2 uid    numeric user ID of file's owner
   4 gid    numeric group ID of file's owner
   8 atime  last access time since the epoch
  16 mtime  last modify time since the epoch
  32 ctime  inode change time (NOT creation time!) since the epo

The default of this package is 0.

If no parameters specified, nothing is set (only current value is returned).

usage
  preserve_stat(1);
parameters
  * optional flag (currently 0 or 1)
returns
Previous value.