in reply to Open a folder
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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Open a folder
by Dr Manhattan (Beadle) on Jan 08, 2013 at 13:26 UTC | |
by marto (Cardinal) on Jan 08, 2013 at 14:28 UTC | |
by clueless newbie (Curate) on Jan 08, 2013 at 16:49 UTC | |
by vinoth.ree (Monsignor) on Jan 09, 2013 at 05:12 UTC | |
by Dr Manhattan (Beadle) on Jan 09, 2013 at 12:04 UTC | |
by vinoth.ree (Monsignor) on Jan 09, 2013 at 13:24 UTC |