
File::Tools - UNIX tools implemented as Perl Modules and made available to other platforms as well

use File::Tools qw(:all);
my $str = cut {bytes => "3-7"}, "123456789";

This is Alpha version of the module. Interface of the functions will change and some of the functions might even disappear.

Why this module?
The goal of this module is to make it even easier to write scripts in Perl that were traditionally easier to write in Shell.
Partially we will provide functions similar to existing UNIX commands and partially we will provide explanation on how to rewrite various Shell constructs in Perl.

Not implemented.
Given a path to a file or directory returns the last part of the path.
See File::Basename for details.
Not implemented.
See slurp
To process all the files on the command line and print them to the screen.
while (my $line = <>) {
print $line;
}
In shell cut is usually used to concatenate two or more files. That can be achived with the previous code redirecting it to a file using > command line redirector.
Concatenating parts of a path in a platform independent way. See also File::Spec
Use the built in chdir function.
Use the built in chmod function.
For now use the built in chown function.
It accepts only UID and GID values, but it is easy to retreive them:
chown $uid, $gid, @files; chown getpwnam($user), getgrname($group), @files;
For recursive application use the find function.
find( sub {chown $uid, $gid, $_}, @dirs);
Windows: See chmod above.
See compare
Compare two files See File::Compare for details.
Not implemented.
See some of the external modules
Copy one file to another name.
For details see File::Copy
For now this does not provide recourseive copy. Later we will provide that too using either one of these modules: File::NCopy or File::Copy::Recursive.
Partially implemented but probably will be removed.
Returns some of the fields of a given string (or strings). As a UNIX command it can work on every line on STDIN or in a list of files. When implementing it in Perl the most difficult part is to parse the parameters in order to account for all the overlapping possibilities which should actually be considered as user error.
cut -b 1 file cut -b 3,7 file cut -b 3-7 file cut -b -4,7- order within the parameter string does not matter
The same can be done in Perl for any single range: substr $str, $start, $length;
See copy instead.
Returns the current working directory similar to the pwd UNIX command.
See Cwd for details.
Can be used to display time in the same formats the date command would do.
See POSIX::strftime for details.
Not implemented.
Not implemented.
See Text::Diff for a possible implementation.
Given a path to a file or a directory this function returns the directory part. (the whole string excpet the last part)
See File::Basename for details.
Not implemented.
Not implemented.
Not implemented.
Not implemented.
The print function in Perl prints to the screen (STDOUT or STDERR).
If the given string is in double quotes "" the backslash-escaped characters take effect (-e mode).
Within single quotes '', they don't have an effect.
For printing new-line include \n withn the double quotes.
Not implemented.
Not implemented.
In Perl there is no need to use a special function to evaluate an expression.
Not implemented.
This is not a UNIX command but it is provided by the same standard File::Basename we already use.
See File::Find for details.
See also find2perl
TODO: Probably will be replaced by File::Find::Rule
See Net::FTP
Move a file from one directory to any other directory with any name.
One can use the built in rename function but it only works on the same filesystem.
See File::Copy for details.
Not implemented.
See Getops::Std and Getops::Long for possible implementations we will use here.
Not implemented.
A basic implementation of grep in Perl would be the following code:
my $p = shift;
while (<>) {
print if /$p/
}
but within real code we are going to be more interested doing such operation on a list of values (possibly file lines) already in memory in an array or piped in from an external file. For this one can use the grep build in function.
@selected = grep /REGEX/, @original;
TODO: See also File::Grep
Not implemented.
Not implemented.
Normally the id command shows the current username, userid, group and gid. In Perl one can access the current ireal UID as $< and the effective UID as $>. The real GID is $( and the effective GID is $) of the current user.
To get the username and the group name use the getpwuid($uid) and getpwgrid($gid) functions respectively in scalar context.
See built in kill function.
Not implemented.
This is used in interactive mode only. No need to provide this functionality here.
Not implemented.
See the build in link and symlink functions.
Not implemented.
See glob and the opendir/readdir pair for listing filenames use stat and lstat to retreive information needed for the -l display mode of ls.
Sending e-mails.
See Mail::Sendmail and Net::SMTP
Not implemented.
See the built in mkdir function.
See also "mkpath"
Create a directory with all its parent directories. See File::Path for details.
Not implemented.
This is used in interactive mode only. No need to provide this functionality here.
See move instead.
Not implemented.
Not implemented.
See Net::Ping
Change directory to last place where pushd was called.
Change directory and save the current directory in a stack. See also popd.
Not implemented.
See the build in printf function.
Not implemented.
See cwd instead.
Not implemented.
read x y z
will read in a line from the keyboard (STDIN) and put the first word into x, the second word in y and the third word in z
In perl one can implement similar behavior by the following code:
my ($x, $y, $z) = split /\s+/, <STDIN>;
Not implemented.
For removing files, see the built in unlink function.
For removing directories see the built in rmdir function.
For removing trees (rm -r) see rmtree
See also File::Remove
Not implemented.
For removing empty directories use the built in rmdir function.
For removing tree see rmtree
Removes a whole directory tree. Similar to rm -rf. See also File::Path
See also Net::SCP
Not implemented.
Not implemented.
Not implemented.
See the built in sort function.
Not implemented.
Return the last n lines of a file, n defaults to 10
Not implemented.
See Archive::Tar
See also Benchmark
Not implemented.
Not implemented.
See the built in tr function.
Not implemented.
The uniq unix command eliminates duplicate values following each other but does not enforce uniqueness through the whole input. For examle for the following list of input values: a a a b a a a ths UNIX uniq would return a b a
For completeness we also provide uniqunix that behaves just like the UNIX command.
See also Array::Unique
Similar to the UNIX uniq command.
Not implemented.
Not implemented.
Not implemented.
Not implemented.
Not implemented.
< > < |
Ctr-Z, & fg, bg set %ENV
$#, $*, $1, $2, ...
$$ - is also available in Perl as $$
$? error code of last command
if test ... string operators

The Subversion repository is here: http://svn1.hostlocal.com/szabgab/trunk/File-Tools/

File::Basename::fileparse_set_fstype File::Compare::compare_text File::Compare::cmp File::Copy::syscopy File::Find File::Spec File::Temp

Gabor Szabo <gabor@pti.co.il>

Copyright 2006 by Gabor Szabo <gabor@pti.co.il>.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html

Tim Maher has a book called Miniperl http://books.perl.org/book/240 that might be very useful. I have not seen it yet, but according to what I know about it it should be a good one.
http://perllinux.sourceforge.net/
The UNIX Reconstruction Project, http://search.cpan.org/dist/ppt/
Related Discussions: