ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

How to display list of files in a directory using Perl?

Here, we will show you to display the list of files in a particular directory using Perl?
Submitted by Godwill Tetah, on December 12, 2020

We will use a Perl inbuilt function known as DIR which means directory handler just as we also have File Handler.

This built-in Perl module has the ability to open a directory and read the content of the directory. So today, we will code a simple program that opens a directory a list the content of the directory, meaning its files and folders.

We will use assign the DIR output to an array and then use the foreach loop to get a list of the directory's content.

The code below is an implementation of the above explanation.

Program/Source Code:

File name: index.pl

#checking directory content using Perl

opendir( DIR, "E:\\PERL\\" );
@x = readdir(DIR);
foreach (@x) {
    print "$_\n";
}
closedir(DIR);

Below are some points to take note of;

  • Notice the way I wrote my directory.
  • The body of the foreach loop has the symbol $_ which represents the content of the array.
  • Finally, the directory is closed using closedir(DIR).

Output:

Display list of files in a directory using Perl

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.