Free Perl Tutorials

PERL chdir function

What does the chdir function do in PERL? Simple! It changes the directory you are in!

Here is an example of how you would use the chdir function in PERL:


#First, we open our main directory
opendir(DIR,'test');

#Then we navigate to a subdirectory
chdir 'subtest';

#Now we would be in the 'subtest' folder or directory
#We can navigate back up to the 'test' folder or directory
chdir '..';


The ".." in above example tells PERL to navigate to the folder or directory directly above our current folder or directory.

PERL opendir Function

"opendir" opens a directory on your computer using PERL. Here is an example of the proper usage of the opendir function:

opendir(DIRECTORY/FOLDER HANDLE, 'directory/folder name');

Think of the DIRECTORY/FOLDER HANDLE just as you would a FILE HANDLE, it acts similar to a variable that you can call on request in the remainder of your script. In a real world example, the example above would probably look something like this:

opendir(DIR, 'test');

Naturally, you would also want to close the folder or directory, so the coding for that would be:

closedir(DIR);

Hope this helps!

Navigate Directories Using PERL

So we've covered how to handle files, now its time to manage directories or folders in PERL! The process is fairly straightforward, and will take you little if any time to learn.

For this exercise, go into your C:/Test folder and create 2 test folders, we'll call them "test1" and "test2".

So our structure is as follows: C:/Test/test1 and C:/Test/test2. Now in the "test1" folder, I have place a few files so we can demonstrate how to view all files associated with a directory or folder:

HOW TO OPEN A DIRECTORY OR FOLDER USING PERL

Here is the code to open a directory and view the files in it using PERL:

#opendir opens the correct folder or directory
opendir(DIR, 'test');

#@files creates an array of all the files in the folder or
#directory while'readdir' reads the folders contents.
@myfiles = readdir(DIR);

#close the directory or folder
closedir(DIR);

#prints the files in the directory
print "@myfiles\n";


So, as you can see, the code to open a file or open a directory is very similar!

HOW TO CHANGE A DIRECTORY OR FOLDER USING PERL

This code is very similar to what is above. After you've already opened the primary directory, simply use the chdir function to navigate to a new subfolder.

Here is an example of how to use perl to change to a different subfolder:

opendir(DIR,'test');
chdir 'test1';
closedir(DIR);


We use "chdir" to change directories in PERL! Note: To navigate to the folder above your current one, you can simply place a '.' in the position of the folder you wish to navigate to!

HOW TO ADD A DIRECTORY OR FOLDER USING PERL

This is the code to add a folder to a directory:

opendir(DIR, 'test');
mkdir 'test33';
close(DIR);


We use the "mkdir" function to create a new directory in PERL!

Here is an example of the code used to remove a directory:

opendir(DIR, 'test');
rmdir 'test2';
close(DIR);



HOW TO REMOVE A DIRECTORY OR FOLDER USING PERL

Here is an example of the code used to remove a directory:

opendir(DIR, 'test');
rmdir 'test2';
close(DIR);


To delete a folder or directory using PERL, use the "rmdir" function!

Now you can move around open, create, remove or delete folders and directories in perl. To test your ability to navigate around your system using perl, you can add an array and print that array to your screen.

stat function in PERL

What is the stat function in PERL? The stat function is a great little tool that returns the statistics on a particular file, expression, or directory. It lists the information on any one of 13 elements from a file, expression, or directory. The 13 elements to choose between are listed below:

0 dev device number of filesystem
1 ino inode number
2 mode file mode (type and permissions)
3 nlink number of (hard) links to the file
4 uid numeric user ID of file's owner
5 gid numeric group ID of file's owner
6 rdev the device identifier (special files only)
7 size total size of file, in bytes
8 atime last access time in seconds since the epoch
9 mtime last modify time in seconds since the epoch
10 ctime inode change time in seconds since the epoch (*)
11 blksize preferred block size for file system I/O
12 blocks actual number of blocks allocated

stat function usage:

$example = (stat $file-to-get-stats-from)[number of the element you want returned from above list];

So, your actual code might look something like this for the stat function in PERL:

$example = (stat $file)[10];


That's all there is to using the stat function in PERL!

How To View Stats On File In PERL

Perhaps you are looking for how large your file size is? Here is how we can find the size of a file using PERL!

First, we choose which file we want to check the statistics on using PERL. In this case, we'll create a file called "test.txt", and use that one. The stats function (or statistics function) in PERL is very versatile in that it can return any one of 13 stats on the file listed below:

0 dev device number of filesystem
1 ino inode number
2 mode file mode (type and permissions)
3 nlink number of (hard) links to the file
4 uid numeric user ID of file's owner
5 gid numeric group ID of file's owner
6 rdev the device identifier (special files only)
7 size total size of file, in bytes
8 atime last access time in seconds since the epoch
9 mtime last modify time in seconds since the epoch
10 ctime inode change time in seconds since the epoch (*)
11 blksize preferred block size for file system I/O
12 blocks actual number of blocks allocated

Yes there are 13 values...remember we count from 0 in PERL!

Ok, so now we want to know the code to have the program look at the file and return whatever stat we want. For this example, we'll look at the file size of a particular file:

#First, we set the file we want to check equal to a string
$myfile = "test.txt";

#Next we call the function that we want (in this case it is the 7th
#element of the stat function in PERL)
$size = (stat $myfile) [7];

#Print the results
print = "My file is $size bytes large\n";

Now if we wanted to know when a file was last accessed, we simply change the 7 to an 8! Easy, right?

How To Delete A File In PERL

Deleting a file in perl is a very simple task. It isn't like the copy function where you have to worry about adding a PERL module to your coding. Nope! This is just good old fashioned coding!

Here is the code to delete a file using PERL:


unlink "test.txt";


What is the PERL code doing?
"unlink" is telling PERL to delete the file named "test.txt" file.

Not exactly the longest bit of PERL coding in the world, but effective nonetheless! Just make sure that you are deleting the correct file when using PERL. If you cut and paste a lot of code, it's easy to overlook the file name you want to delete and delete the wrong file. Don't ask how I know this!

How To Copy A File In PERL

HOW TO COPY A FILE IN PERL

Copying a file using the PERL programming language is a bit more complicated than the renaming function. It's got a small difference, but nothing even a newb like you can't over come :)

Here is the code for how to copy a file in PERL:


use File::Copy
copy ("test.txt","copy-of-test.txt");


*GASP*
What in the world is that crazy "use" function, and what the heck is "File::Copy"??!
Relax. PERL has a library of modules that you can use in your programming endeavors to make your life 100 times easier. "use" tells PERL to 'use' a module called File::Copy (which most likely came with your original download of PERL). Since PERL in its most naked form doesn't have an actual copy function, it allows you to use the "copy" function we see in our code!

Now to explain the code: The first .txt file in the quotes in the parenthases (test.txt) is the file we want to copy and the second in quotes in the parenthases is the name of the copied file we wish to create (copy-of-test.txt).

Run the program, and you will see your original file and the copied file.

Don't see a copied file? Probably because your copy .pl program is not located in the same directory as the .txt file you wish to copy!

Don't fear the modules! Modules are our friend!