title: iterating over all the subdirectories looking for XML files - Perl
body: <p>I am trying to implement a procedure that iterates over all the <code>subdirectories</code> from a <code>root</code> directory, looking for <code>.xml</code> files in <code>Perl</code>. </p>
  
  <pre><code>sub getXMLFiles {
  
      my $current_dir = $_[ 0 ];
      opendir my $dir, $current_dir or die "Cannot open directory: $!\n";
      my @files = grep /\.xml$/i, readdir $dir;
      closedir $dir;
  
      return @files;
  }
  
  sub iterateDir {
      my $current_dir = $_[ 0 ];
      finddepth( \&amp;wanted, $current_dir );
      sub wanted{ print getXMLFiles }
  }
  #########################################################
  #                                                       #
  # define the main subroutine.                           #
  # first, it figures from where it is being ran          #
  # then recursively iterates over all the subdirectories #
  # looking for .xml files to be reformatted              #
  #                                                       #
  #########################################################
  sub main(){
      #
      # get the current directory in which is the 
      # program running on
      #
      my $current_dir = getcwd;
      iterateDir( $current_dir );
  }
  
  #########################################################
  #                                                       #
  # call the main function of the program                 #
  #                                                       #
  #########################################################
  main();
  </code></pre>
  
  <p>I'm not very familiar with <code>Perl</code>. The <code>sub iterateDir</code> procedure is supposed to iterate over a the sub-directories, while the <code>getXMLFiles</code> would filter for <code>.xml</code> files, returning them. I would use those <code>.xml</code> files for parsing.  That's why I'm trying to find all the <code>.xml</code> files from a <code>root</code> directory.
  However, I don't know how can I use the <code>sub wanted</code> procedure inside <code>iterateDir</code> to send the <code>dirpath</code> to <code>getXMLFiles</code>. How could I accomplish that? </p>