Lecture#13
Chapter 13 - Manipulating Files and Directories


Removing Files
In Unix, rm file1 file2 is the Perl equivalent of:

unlink "file1", "file2";

The return value is the number of files deleted...

$num = unlink glob "*.txt";
print "$num files were deleted.\n";

unlink will set $! on a failure..it returns the number of files deleted. You should try and delete files one at a time and test to see if the function returns 1 in order not to miss any deletion errors.


Renaming Files
In unix, you would rename a file using the mv command...mv oldfile newfile...in Perl...

rename "oldfile", "newfile";

rename will set $! on a failure. It returns false if it fails.


Linking Files
Hard Links are like aliases for a file. In unix, ln file1 file2, in Perl....

link "file1", "file2"; #cant link to a directory though

Symbolic or softlinks are like pointers to files...in Unix, ln -s file1 file2...in Perl....

symlink "thefile_or_dir", "thelink";

Both of these set $! on a failure.
When you link something, you have to delete all the names to completely delete the file.
When you symlink something, deleting the file can leave a 'dangling link', deleting the link does just that.

If something is a symlink,

readlink "/tmp/mylink"; #Will return where the link points or undef if it's not a link


Making/Removing Directories
Making a directory....
mkdir "dirname", 0755; #makes a directory with 755 permissions, easier to be in octal

$permissions = "0755";
mkdir "dirname", oct($permissions); #converts that string to octal

Removing a directory....
rmdir "dirname"; #Just like unix...fails on non-empty directories

These set $! on a failure.


Changing Permissions, Ownership, Timestamps
Changing Permissions
chmod 0755, "file1", "file2"; #returns items successfully changed, sets $!

Changing Ownership
Changing ownership is a little tricker than what you have to do in unix, because Perl's chown must use the numeric user and group id...luckily, there are functions provided for this!
$user = getpwnam "theuser";
$group = getgrnam "thegroup";
chown $user, $group, "file_or_files";

Returns number of files altered, sets $! on failure

Changing Timestamps
Similar to the Unix touch command
$accessed = time; #Get the numeric value of right now
$modified = time + 60; #Set this variable to now + 1 minute
utime $accessed, $modified, "file_or_files";


Using Simple Modules
We're only mentioning this section so you'll be able to understand some code you may see...we won't be using many modules....

Modules are like code that poeple have written and provided for your use...kind of like libraries in other languages.

The module we'll use as an example is the one in the book: File::Basename.

To make the code available to your program, put the following line below the #! directive...

use File::Basename;

This provides several new functions for you to call...for example,

$basename = basename "/etc/security/myfile"; #Calling File::Basename's function basename

There are also some 'object oriented' modules...same idea, but the functions are called methods and are called differently....

use File::Spec; #This one is OO and has a method named catfile that takes 2 args...
$filename = File::Spec->catfile("/etc/security/", "myfile"); #Notice the use of ->


CSC255 - Alan Watkins - North Carolina State University