Perl Directories
w3hello.com logo
Perl Basics
Perl Introduction
Perl Installation
Perl Comments
Perl String
Perl Operators
Perl Variables
Perl Arrays
Perl do...while Loop
Perl Environment
Perl Syntax Overview
Perl while Loop
Perl Array with Loops
Perl IF...ELSE statement
Perl IF Statement
Perl if-else
Perl foreach Loop
Perl for Loop
Perl continue Statement
Perl nested Loop
Perl until Loop
Perl next Statement
Perl Array
Perl Loops
Perl File I/O
Perl goto
Perl Socket Programming
Perl Error Handling
Perl Directories
Perl Process Management
Perl stdin
Real Device Vs Emulator Testing: Ultimate Showdown
Perl Formats Writing Reports
Perl Regular Expressions
Perl Create Excel File
A complete guide to CSTE certification
Perl Hashes
Perl Special Variables
Perl do-while Loop
Perl Chop() Vs Chomp()
The Perl DBI
Perl IF...ELSIF statement
Perl print() and say()
Perl Database Access
Perl Formats
Perl last Statement
Perl UNLESS...ELSE statement
Perl Scalars
How to organize Requirements as a Business Analyst
Perl Packages and Modules
Perl goto Statement
Perl Object Oriented
Perl Subroutines
Perl redo Statement
Perl Command Line Arguments | argv
Perl String Escaping Characters
Perl Date and Time
Perl Modules and namespaces
Perl UNLESS...ELSIF statement
Perl Conditional Statements IF...ELSE
Perl Functions and Subroutines
Perl Sending Email
Perl DBI
Perl Versions
Top 100 QTP Interview Questions & Answers
Perl Operator Types
Perl switch Statement
Perl File handling
Perl References
Perl CGI Programming
Object Oriented Programming in PERL
Perl switch
Perl Interview Questions
How PERL is used in Automation Testing
Perl Functions References
Perl Multidimensional Array
Perl unless Statement
Perl Basics
Previous : Perl Error Handling Next : Perl Process Management

Perl Directories


Different operating systems have different commands to look at the files list within a directory. For example, 'li' command is used for Linux and 'dir' command is used for Windows.

But we can also open a directory in Perl using Perl directory functions. Here, Directory handling is quite similar to file handling.

Perl provides two alternatives for directories: file globbing and directory functions. File globbing is usually used for a similar subset of files whereas directory functions provide more options.

Perl Directory Functions

In the given table, we have listed some of the directory functions.

Function Usage
opendir DIRHANDLE, EXPR It opens a directory.
readdir DIRHANDLE It reads a directory.
rewinddir DIRHANDLE It positions pointer to the beginning.
telldir DIRHANDLE It returns current position of the dir
seekdir DIRHANDLE, POS It points pointer to POS inside dir.
closedir DIRHANDLE It closes a directory.

Perl Open/Display Directory

To open a directory in Perl, there is a function opendir. This function returns true on success or false on failure.

Setting $! with actual error message will display a reason for the failure.

Syntax:

Example:

Output:

Perl Directory 1

Perl Create Directory

To create a new directory in Perl, there is a function mkdir. You need required permission to create a directory.

Syntax:

Example:

Output:

Directory created successfully

Given snapshot shows created directory perl in the /users/w3hello directory list.

Perl Directory 2

Perl Read Directory in SCALAR context

To read content of a directory, function readdir is used. In scalar context, this function will return each item of a directory one by one. Once everything is read out, it will return undef.

Syntax:

Example:

Output:

Perl Directory 3

Perl Read Directory in LIST context

In list context, readdir function will return all the content of directory in one statement. So it uses more memory.

Syntax:

Example:

Output:

Perl Directory 4

Perl Remove Directory

To remove a directory in Perl, there is a function rmdir. You need required permission to remove a directory. The directory which you want to remove should be empty before removing it.

Syntax:

Example:

Output:

Directory removed successfully

Perl Change Directory

To change a directory in Perl, there is a function chdir. To change a directory and go inside a new directory you need required permission.

Syntax:

Example:

Output:

Diretory has been changed to /users/

Perl Close Directory

To close a directory in Perl, there is a function closedir. This function officially shut down the connection between directory handle and directory. Although, closing directory is not mandatory in Perl because directory will be automatically closed when variable goes out of scope.

Syntax:

Perl Opening Directory using File Globbing

To open all files in a directory matching a specific pattern, use Perl filename glob matching pattern syntax.

We'll display the list of all the files having extension .docx in a directory.

Output:

Perl Directory 5

In the above output, all the files are read in the directory, but grep only passes files with .docx extension.

Previous : Perl Error Handling Next : Perl Process Management
© Copyright 2018 W3Hello Publishing Limited. All rights reserved.
Contact us | Sitemap