Fandom

How To Wiki

How to find the contents of a directory using PERL

1,795pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

To find the contents of a directory in perl use the opendir function.

  • opendir()
    • Operats similar to the open file function open()
    • format: opendir(VARIABLE, '/full/directory/or/relative
    • Example: opendir(DIR, '.');
      open the current directory you are in and set it as variable DIR
  • readdir()
    • reads the contents of the directory
  • closedir()
    • closes the variable set for the directory read


  • practical example
    • This finds all jpgs and gifs in a directory and displays them for a website
opendir(DIR, '.');
while (defined($dir = readdir(DIR))) {
if ( $dir =~ /\.jpg/i || $dir =~ /\.gif/i)
{ 
print "<center><img src=\"$dir\"></center><br><br>\n";
}
}
closedir(DIR);


From HowTo Wiki, a Wikia wiki.

Also on Fandom

Random Wiki