title: Perl recursively copy files of specific format
body: <p><em>Note: The related question is solved by use of a deprecated module and is not consistent across OS. The answer to current question uses newer modules and hence is being posted here.</em></p>
  
  <p>I have a module that in turn uses the <a href="http://search.cpan.org/dist/File-NCopy/" rel="nofollow noreferrer">File::NCopy</a> CPAN module for recursively copying files from  to . </p>
  
  <p>The problem is - I need to recursively copy only the files of specific file type to the destination. Is there any way to filter the source by extension? </p>
  
  <p>As an alternative - is there a way to copy all files except the hidden files? My main problem being the .git folder also gets copied - which is not desired.</p>
  
  <p>Platform: MacOS</p>
  
  <p>Alternatives explored: </p>
  
  <p>1) File::Copy::Recursive module :- seems to provide only recursive copy of files or directories. Does not seem to help with either hidden files or exclude filter</p>
  
  <p>2) Using rsync -avz --exclude=".*" :- unable to combine this with recursive copy functionality.</p>
  
  <p>3) Homegrown solution similar to <a href="https://stackoverflow.com/questions/227613/how-can-i-copy-a-directory-recursively-and-filter-filenames-in-perl">How can I copy a directory recursively and filter filenames in Perl?</a> :- Might be the last resort - but does not seem portable unless tweaked and tested across different platforms. <strong>Will be falling back to this unless a module already exists.</strong></p>
  
  <p>4) <a href="https://metacpan.org/pod/Path::Class::Dir" rel="nofollow noreferrer">https://metacpan.org/pod/Path::Class::Dir</a> :- Seems plausible - will be running a quick implementation using this.</p>
  
  <p><strong>Implemented Solution:</strong></p>
  
  <p>I used the recursive module and the Path::Class::Dir</p>
  
  <pre><code> dir($sourceDir)-&gt;recurse(callback =&gt; sub {
       my $file = shift;
  
       return if($file eq '.' || $file eq '..');
  
       return if -d $file;
  
       if (&lt;custom filter&gt;)
       {
           my $path = file($file)-&gt;relative($sourceDir);
           fcopy("$sourceDir/$path", "$destinationDir/$path") or die "Could not perform fcopy: $!";
       }
   });
  </code></pre>
  
  <p>Without the relative path - the destination folder structure does not seem to be the same as the source folder structure.</p>