NAME

File::Util - Easy, versatile, portable file handling

VERSION

version 4.130590

DESCRIPTION

File::Util provides a comprehensive toolbox of utilities to automate all kinds of common tasks on files and directories. Its purpose is to do so in the most portable manner possible so that users of this module won't have to worry about whether their programs will work on other operating systems and/or architectures. It works on Linux, Windows, Mac, BSD, Unix and others.

File::Util is written purely in Perl, and requires no compiler or make utility on your system in order to install and run it.

File::Util also aims to be as backward compatible as possible, running without problems on Perl installations as old as 5.006. You are encouraged to run File::Util on Perl version 5.8 and above.

After browsing this document, please have a look at the other documentation. (See DOCUMENTATION section below.)

SYNOPSIS

# use File::Util in your program
use File::Util;

# create a new File::Util object
my $f = File::Util->new();

# load a file into a variable
my $content = $f->load_file( 'some_file.txt' );

# write content to a file
$f->write_file( 'another_file.txt' => $content );

# get the contents of a directory, 3 levels deep
my @songs = $f->list_dir( '~/Music' => { recurse => 1, max_depth => 3 } );

DOCUMENTATION

You can do much more with File::Util than the examples above. For an explanation of all the features available to you, take a look at these other reference materials:

The "Nutshell"

The File::Util::Manual::Examples document has a long list of small, reusable code snippets and techniques to use in your own programs. This is the "cheat sheet", and is a great place to get started quickly. Almost everything you need is here.

The Manual

The File::Util::Manual is the complete reference document explaing every available feature and object method. Use this to look up the full information on any given feature when the examples aren't enough.

The Cookbook

The File::Util::Cookbook contains examples of complete, working programs that use File::Util to easily accomplish tasks which require file handling.

BASIC USAGE

Getting Started

# use File::Util in your program
use File::Util;

# ...you can optionally enable File::Util's diagnostic error messages:
# (see File::Util::Manual section regarding diagnostics)
use File::Util qw( :diag );

# create a new File::Util object
my $f = File::Util->new();

# ...you can enable diagnostics for individual objects:
$f = File::Util->new( diag => 1 );

File Operations

# load content into a variable, be it text, or binary, either works
my $content = $f->load_file( 'Meeting Notes.txt' );

# ...or do it with diagnostics, for just for this call
$content = $f->load_file( 'Meeting Notes.txt' => { diag => 1 } );

# wrangle some text
$content =~ s/this/that/g;

# re-write the file with your changes
$f->write_file(
   file => 'Meeting Notes.txt',
   content => $content,
);

# try binary this time
my $binary_content = $f->load_file( 'barking-cat.avi' );

# get some image data from somewhere...
my $picture_data = get_image_upload();

# ...and write a binary image file, using some other options as well
$f->write_file(
   file => 'llama.jpg',
   content => $picture_data,
   { binmode => 1, bitmask => oct 644 }
);

# ...or write a file with UTF-8 encoding (unicode support), using the
# short notation: write_file( [file name] => [content] => { options } )
$f->write_file( 'encoded.txt' => qq(\x{c0}) => { binmode => 'utf8' } );

# load a file into an array, line by line
my @lines = $f->load_file( 'file.txt' => { as_lines => 1 } );

# see if you have permission to write to a file, then append to it
if ( $f->is_writable( 'captains.log' ) ) {

   my $fh = $f->open_handle(
      file => 'captains.log',
      mode => 'append'
   );

   print $fh "Captain's log, stardate 41153.7.  Our destination is...";

   close $fh or die $!;
}
else { # ...or warn the crew

   warn "Trouble on the bridge, the Captain can't access his log!";
}

# get the number of lines in a file
my $log_line_count = $f->line_count( '/var/log/messages' );

File Handles

# get an open file handle for reading
my $fh = $f->open_handle( file => 'Ian likes cats.txt', mode => 'read' );

while ( my $line = <$fh> ) { # read the file, line by line

   # ... do stuff
}

close $fh or die $!; # don't forget to close ;-)

# get an open file handle for writing
$fh = $f->open_handle(
   file => 'John prefers dachshunds.txt',
   mode => 'write'
);

print $fh 'Shout out to Bob!';

close $fh or die $!; # don't forget to close ;-)

Directories

# get a listing of files, recursively, skipping directories
my @files = $f->list_dir( '/var/tmp' => { files_only => 1, recurse => 1 } );

# get a listing of text files, recursively
my @textfiles = $f->list_dir(
   '/var/tmp' => {
      files_match => qr/\.txt$/,
      files_only  => 1,
      recurse     => 1,
   }
);

# walk a directory, using an anonymous function or function ref as a
# callback (higher order Perl)
$f->list_dir( '/home/larry' => {
   recurse  => 1,
   callback => sub {
      my ( $selfdir, $subdirs, $files ) = @_;

      print "In $selfdir there are...\n";

      print scalar @$subdirs . " subdirectories, and ";
      print scalar @$files   . " files\n";

      for my $file ( @$files ) {

         # ... do something with $file
      }
   },
} );

# get an entire directory tree as a hierarchal datastructure reference
my $tree = $f->list_dir( '/my/podcasts' => { as_tree => 1 } );

Getting Information About Files

print 'My file has a bitmask of ' . $f->bitmask( 'my.file' );

print 'My file is a ' . join(', ', $f->file_type( 'my.file' )) . " file.";

warn 'This file is binary!' if $f->is_bin( 'my.file' );

print 'My file was last modified on ' .
   scalar localtime $f->last_modified( 'my.file' );

Getting Information About Your System's IO Capabilities

# Does your running Perl support unicode?
print 'I support unicode' if $f->can_utf8;

# Can your system use file locking?
print 'I can use flock' if $f->can_flock;

# The correct directory separator for your system
print 'The correct directory separator for this system is ' . $f->SL;

# Does your platform require binmode for all IO?
print 'I always need binmode' if $f->needs_binmode;

# Is your system an EBCDIC platform?  (see perldoc perlebcdic)
print 'This is an EBCDIC platform, so be careful!' if $f->EBCDIC;

...See the File::Util::Manual for more details and features like advanced pattern matching in directories, callbacks, directory walking, user-definable error handlers, and more.

PERFORMANCE

File::Util has been optimized to run fast.* In many scenarios it can out-perform other modules (like some of those listed in the SEE ALSO section) from anywhere from 100%-400% -- (See the benchmarking and profiling scripts that are included as part of this distribution.)

File::Util consists of several modules, but only loads the ones it needs when it needs them and also offers a comparatively fast load-up time, so using File::Util doesn't bloat your code footprint.

*Sometimes it doesn't matter how fast you can search through a directory 1000 times. Performance alone isn't the best criteria for choosing a module.

METHODS

File::Util exposes the following public methods.

Each of which are covered in the File::Util::Manual, which has more room for the detailed explanation that is provided there.

This is just an itemized table of contents for HTML POD readers. For those viewing this document in a text terminal, open perldoc to the File::Util::Manual.

atomize_path (see atomize_path)
bitmask (see bitmask)
can_flock (see can_flock)
can_utf8 (see can_utf8)
created (see created)
diagnostic (see diagnostic)
ebcdic (see ebcdic)
escape_filename (see escape_filename)
existent (see existent)
file_type (see file_type)
flock_rules (see flock_rules)
is_bin (see is_bin)
is_readable (see is_readable)
is_writable (see is_writable)
last_access (see last_access)
last_changed (see last_changed)
last_modified (see last_modified)
line_count (see line_count)
list_dir (see list_dir)
load_dir (see load_dir)
load_file (see load_file)
make_dir (see make_dir)
abort_depth (see abort_depth)
needs_binmode (see needs_binmode)
new (see new)
onfail (see onfail)
open_handle (see open_handle)
read_limit (see read_limit)
return_path (see return_path)
size (see size)
split_path (see split_path)
strip_path (see strip_path)
touch (see touch)
trunc (see trunc)
unlock_open_handle (see unlock_open_handle)
use_flock (see use_flock)
valid_filename (see valid_filename)
write_file (see write_file)

EXPORTED SYMBOLS

Exports nothing by default. File::Util fully respects your namespace. You can, however, ask it for certain things (below).

EXPORT_OK

The following symbols comprise @File::Util::EXPORT_OK), and as such are available for import to your namespace only upon request. They can be used either as object methods or like regular subroutines in your program.

To get any of these functions/symbols into your namespace without having to use them as an object method, use this kind of syntax:

use File::Util qw( strip_path NL );

* atomize_path      * can_flock         * can_utf8
* created           * diagnostic        * ebcdic
* escape_filename   * existent          * file_type
* is_bin            * is_readable       * is_writable
* last_access       * last_changed      * last_modified
* needs_binmode     * return_path       * size
* split_path        * strip_path        * valid_filename
* NL and SL

EXPORT_TAGS

:all (imports all of @File::Util::EXPORT_OK to your namespace)

:diag (imports nothing to your namespace, it just enables diagnostics)

You can use these tags alone, or in combination with individual symbols as shown above.

PREREQUISITES

No External Prerequisites

File::Util only depends on modules that are part of the Core Perl distribution

Perl 5.8.1 or better ...

You can technically run File::Util on older versions of Perl 5, but it isn't recommended, especially if you want unicode support and wish to take advantage of File::Util's ability to read and write files using UTF-8 encoding.

INSTALLATION

To install this module type the following at the command prompt:

perl Build.PL
perl Build
perl Build test
sudo perl Build install

On Windows systems, the "sudo" part of the command may be omitted, but you will need to run the rest of the install command with Administrative privileges

BUGS

Send bug reports and patches to the CPAN Bug Tracker for File::Util at rt.cpan.org

SUPPORT

If you want to get help, contact the authors (links below in AUTHORS section)

I fully endorse http://www.perlmonks.org as an excellent source of help with Perl in general.

CONTRIBUTING

The project website for File::Util is at https://github.com/tommybutler/file-util/wiki

The git repository for File::Util is on Github at https://github.com/tommybutler/file-util

Clone it at git://github.com/tommybutler/file-util.git

This project was a private endeavor for too long so don't hesitate to pitch in.

AUTHORS

Tommy Butler http://www.atrixnet.com/contact

Others Welcome!

COPYRIGHT

Copyright(C) 2001-2013, Tommy Butler. All rights reserved.

LICENSE

This library is free software, you may redistribute it and/or modify it under the same terms as Perl itself. For more details, see the full text of the LICENSE file that is included in this distribution.

LIMITATION OF WARRANTY

This software is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.

This disclaimer applies to every part of the File::Util distribution.

SEE ALSO

The rest of the documentation: File::Util::Manual, File::Util::Manual::Examples, File::Util::Cookbook,

Other Useful Modules that do similar things: File::Slurp, File::Spec, File::Find::Rule, Path::Class