Perl for System Administration
Perl can be used to perform or automate system administration tasks on all platforms on which it is available. Some UNIX beginners who learn Perl, use it at first where shell scripts may be more appropriate. This use is still OK, because Perl can be a good alternative for them.
Besides the core language, those who wish to use Perl for System Administration should focus on the perlfunc
document (which covers the various functions available inside Perl), and on its regular expressions capabilities (which are handier than what shell scripts offer).
Modules
Useful modules for files and directories handling are:
- Path-Tiny - a module that provides a “fast utility for working with file paths” and which despite its name, provides a comprehensive and rich API.
- File-Slurper - a module for fast and easy input and output from files and directories. The interface is procedural and quite Spartan, but is still useful. (Note: Using File-Slurp, and File-Slurp-Tiny which were found to be broken, is no longer recommended.)
- File::Spec - a core module to handle file and directory paths portably.
- File::Basename - a core module to portably extract the basename, the dirname, the suffix and other file paths parsing.
- String-ShellQuote - quote strings for passing through the shell. Also see the list forms of system.
- File::Path - a core module to create or remove directory trees (portably).
- File::Copy - a core module to copy files.
- IO-All - a no longer recommended all-in-one IO package with a lot of syntactic sugar, but quite a few quirks and bugs. Non-core.
Directory Traversal
The built-in module for traversing a directory tree in Perl is File::Find, but it has some severe limitations in interface and use. Some better alternatives are:
File-Find-Object - an object-oriented replacement for File::Find that: 1) can be instantiated 2) has an iterative interface 3) can be interrupted in the middle and 4) can return result objects instead of path names.
File-Next - an alternative with an iterative interface, but incapable of being instantiated.
File-Find-Rule, which is still based on File::Find, and File-Find-Object-Rule provide a more convenient and succinct interface for finding what you want.
Path-Iterator-Rule provides an object oriented and iterative file finder, and there's also Path-Class-Rule and IO-All-Rule. Also of interest is its “See Also” section, which contains links to other implementations, and a comparison of them.