To open the directory,use a function called "opendir". You use this same like the open function to open files. Below example shows opening of /tmp directory.
Reading the directory#!/usr/bin/perl use strict; use warnings; my $tmp_dir="/tmp"; opendir (DIR, $tmp_dir) or die $!;
To read the files and directories in the directory we use the readdir function. readdir returns the name of each file or directory in the opened directory in turn when used in scalar context, or a list of the names of all files and directories in that directory when used in list context. This means that we can use readdir in a foreach loop or any other loop
Closing the directorywhile (my $file_name = readdir(DIR)) {print "$file_name\n";}
We use the function closedir to close the directory once we are finished with it. Like files, the directory will be closed when the program finish, but sometimes need to close the directory opened
closedir(DIR);
In reply to Re: Open a folder
by vinoth.ree
in thread Open a folder
by Dr Manhattan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |