- Perl Basics
- Perl Home
- Perl Basic Syntax
- Perl Data Types
- Perl Variables
- Perl Scalars
- Perl Arrays
- Perl Hashes
- Perl If-Else
- Perl Loops
- Perl Operators
- Perl References
- Perl Subroutines
- Perl Formats
- Perl Date & Time
- Perl File I/O
- Perl Advanced
- Perl Directory
- Perl Regular Expressions
- Perl Special Variables
- Perl Socket Programming
- Perl Send E-mail
- Perl Object Oriented
- Perl Error Handling
- Perl CGI Programming
- Perl Database
- Perl Packages & Modules
- Perl Process Management
- Perl Test
- Perl Online Test
- Give Online Test
- All Test List
Perl Directory
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 »
Like/Share Us on Facebook 😋