Main Content

Perl - functions for directory handling

Archive - Originally posted on "The Horse's Mouth" - 2007-06-09 07:48:09 - Graham Ellis

Perl has many built in functions for file and directory handling and you should use them in preference to shelling out in your scripts because:

• They work across operating systems (at least as far as is practical)

• They are much more efficient as there's no extra processes being started and stopped all the time.

I've added a source code sample to our web site which shows some of these in use ...

cwd to find out where you are
chdir to change your current directory
-d to check is a symbol is a directory
opendir and readdir to read through a directory's content

Sample output:

earth-wind-and-fire:~ grahamellis$ perl chdirddemo
We are in /Users/grahamellis
We are now in /Users/grahamellis/jun07
Directory entry count: 24
We are now in /Users/grahamellis
Directory entry count: 390
earth-wind-and-fire:~ grahamellis$