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?
When I am writing filesystem related applications I always need to load several standard modules such as File::Basename, Cwd, File::Copy, File::Path and maybe others in order to have all the relevant functions. I'd rather just use one module that will bring all the necessary functions.
On the other hand when I am in OOP mood I want all these functions to be methods of a shell-programming-object. (Though probably Pipe will answer this need better)
There are many useful commands available for the Unix Shell Programmer that usually need much more coding than the Unix counterpart, specifically most of the Unix commands can work recoursively on directory structures while in Perl one has to implement these. There are some additional modules providing this functionality but then we get back again to the previous issue.
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.
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
Compare two files See File::Compare for details.
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.
See Filesys::DiskSpace
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)
Filesys::DiskUsage
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.
In Perl there is no need to use a special function to evaluate an expression.
match
substr - built in substr
index - built in index
length - built in length
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.
See Getops::Std and Getops::Long for possible implementations we will use here.
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
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.
This is used in interactive mode only. No need to provide this functionality here.
See the build in link and symlink functions.
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
See the built in mkdir function.
See also "mkpath"
Create a directory with all its parent directories. See File::Path for details.
See move instead.
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.
See the build in printf function.
See cwd instead.
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>;
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
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
Net::SNMP
Net::SSH
See the built in sort function.
Return the last n lines of a file, n defaults to 10
See Archive::Tar
Net::Telnet
See also Benchmark
See the built in tr function.
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.
< > < |
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/
Pipe
Related Discussions:
http://www.perlmonks.org/?node_id=541826
To install File::Tools, copy and paste the appropriate command in to your terminal.
cpanm
cpanm File::Tools
CPAN shell
perl -MCPAN -e shell install File::Tools
For more information on module installation, please visit the detailed CPAN module installation guide.