Search⌘ K

File Manipulation

Explore how to manipulate files in Perl by using file test operators to check attributes, rename and move files, and manage directories effectively. Learn to use built-in functions and CPAN modules like File::Copy and Cwd to handle file operations and working directories, enhancing your ability to write reliable and maintainable Perl code.

Besides reading and writing files, we can also manipulate them as we would directly from a command line or a file manager.

File test operators

The file test operators, collectively called the -X operators, examine file and directory attributes. To test that a file exists, try this:

Perl
my $filename = 'some_file';
#...
say 'Present!' if -e $filename;
  • The -e operator has a single operand, either the name of a file or a handle to a file or directory. If the file or directory exists, the expression will evaluate to a true value. perldoc -f -X ...