File::Operations - file system specific functions
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);
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.
the Cwd manpage, the File::Basename manpage, the File::Copy manpage, the Encode manpage, the Exception manpage, the Debug manpage.
$content = load_file($filename)
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).
* 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)
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).
save_file($filename, $content);
* 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)
1 on success, otherwise throws exception.
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).
append_file($filename, $content);
* 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)
1 on success, otherwise throws exception.
$field_table = load_field_file($filename)
* 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)
$field_table = save_field_file($filename, $field_table)
* 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)
1 on success, otherwise throws exception.
remove_file($filename);
* filename - name of the file to be deleted
1 on success, otherwise throws exception.
make_dir($PREVIEW_DIR);
* directory to make * optional creating dir permissions (default is $DEFAULT_DIR_PERM)
1 on success, otherwise throws exception.
make_path($PUBLISH_DIR);
* path to make * optional creating dir permissions (default is $DEFAULT_DIR_PERM)
1 on success, otherwise throws exception.
copy_file($from, $to);
* file name to copy from * file name to copy to
1 on success, otherwise throws exception.
move_file($from, $to);
* file name to move from * file name to move to
1 on success, otherwise throws exception.
clean_dir($PREVIEW_DIR);
* 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
1 on success, otherwise throws exception.
clean_dir(3).
remove_dir($TMP_DIR);
* directory to clean
1 on success, otherwise throws exception.
Destination directory must not exist. Use: trap { remove_dir($dest); };
to remove it before copying.
copy_dir($dir_from, $dir_to);
* source directory to copy * destination directory to copy to (may not exist) * optional creating dir permissions (default is $DEFAULT_DIR_PERM)
1 on success, otherwise throws exception.
Destination directory must not exist. Use: trap { remove_dir($dest); };
to remove it before copying.
move_dir($dir_from, $dir_to);
* source directory to move from * destination directory to move to (must not exist)
1 on success, otherwise throws exception.
# mini file lister
foreach my $file (@{list_filenames('/home/ftp')}) {
print "File $file\n" if -f $file;
print "Dir $file\n" if -d $file;
}
* directory to list
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).
default_dir_perm(0700);
* optional default directory permission (integer)
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).
preserve_stat(1);
* optional flag (currently 0 or 1)