display file names in dirs

i have this code that displays the name of all directories below a certain directory but id also like it to print the names of all files with the .txt extension that are below the directory ($dir)

$dir = "/data/local/apache/htdocs/uploads";

opendir DIR,$dir or die "can't open $dir $!";

while( $file=readdir DIR ){
print "file is $file<br>";
print "dirfile is $dir/$file<br>";
next unless -T "$dir/$file";
open FILE,"<$dir/$file" or warn "can't open <$dir/$file $!";
local $/=undef;
$text=<FILE>;

}                                  

thanks

boofullsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

guadalupeCommented:
sub scan_dirs($)
{
      #Make scope relative to sub instance
      my $dir=$_[0];

      #my $nivel = $_[1];

      my (@dirs,@files,@results,$filename,$newdir,$list);


      #Open dir handle
      opendir(dir,$dir);

      #Aplicar filtro con grep al readdir
      @dirs=grep {!(/^\./) && -d "$dir/$_"} readdir(dir);      

      #Order alpabeticamente
      @dirs = sort @dirs;

      rewinddir(dir);

      #Get Files

            @files=grep {!(/^\./) && !(-d "$dir/$_") && (-T "$dir/$_")} readdir(dir);

      #Ordenar Archivos
      @files = sort @files;
      closedir (dir);
      
      #Foreach Dir (Only if you want recursive)
      for $list(0..$#dirs)
      {
            #Armar dir para proximo paso recursivo del sub
            $newdir=$dir."/".$dirs[$list];

            print "Dir: $newdir\n";
            
            
                  #Foreach Dir (Only if you want recursive)
                  #&scan_dirs($newdir);
      }

      foreach $file (@files)
      {
            print "File: $file\n";
      }
}#Fin del Sub Scan_Files

This will print out the dirs and files from a starting point and if you uncomment the line:

#&scan_dirs($newdir);

It will also recurse.

Call the sub by passing it a starting dir like this:

&scan_dirs("/usr/bin/files");
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.