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.
rename "oldfile", "newfile";
rename will set $! on a failure. It returns false if it fails.
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
$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 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";
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 ->