codescracker


perl

Perl Directory



« Previous Tutorial Next Tutorial »


You are free to handle directory using your perl program. There are the following standard functions available in perl used to play with directory:

opendir DIRHANDLE, EXPR  # this is used to open a directory
readdir DIRHANDLE        # this is used to read a directory
rewinddir DIRHANDLE      # this is used to position pointer to the beginning
telldir DIRHANDLE        # this simply returns the current position of the dir
seekdir DIRHANDLE, POS   # this is used to position pointer to the POS inside the dir
closedir DIRHANDLE       # this is used to close a directory

Display all Files from a Directory in Perl

Here is an example program, prints all the files available in the /tmp directory using perl:

#!/usr/bin/perl

# this will display all the files present inside the /tmp directory
$dir = "/tmp/*";
my @files = glob($dir);

foreach (@files)
{
   print($_ . "\n");
}

Here is another perl program, displays all the .cpp source files available in the /tmp directory using perl:

#!/usr/bin/perl

# this will display all the cpp source files present inside the /tmp directory
$dir = "/tmp/*.cpp";
@files = glob($dir);

foreach (@files )
{
   print($_ . "\n");
}

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test