<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>beer planet &#187; robust</title>
	<atom:link href="http://beerpla.net/tag/robust/feed/" rel="self" type="application/rss+xml" />
	<link>http://beerpla.net</link>
	<description>where things have nothing to do with beer - tutorials, tips, how-tos, thoughts, hacks, and other techy nonsense</description>
	<lastBuildDate>Sun, 08 Aug 2010 23:59:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<atom:link rel='hub' href='http://beerpla.net/?pushpress=hub'/>
		<item>
		<title>[Perl] Finding Files, The Fun And Elegant Way</title>
		<link>http://beerpla.net/2009/04/08/perl-finding-files-the-fun-and-elegant-way/</link>
		<comments>http://beerpla.net/2009/04/08/perl-finding-files-the-fun-and-elegant-way/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 14:00:00 +0000</pubDate>
		<dc:creator>Artem Russakovskii</dc:creator>
				<category><![CDATA[Awesomeness]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[efficient]]></category>
		<category><![CDATA[elegant]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[follow]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[robust]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[skip]]></category>
		<category><![CDATA[SVN]]></category>
		<category><![CDATA[symlink]]></category>
		<guid isPermaLink="false">http://beerpla.net/2009/04/08/perl-finding-files-the-fun-and-elegant-way/</guid>
		<description><![CDATA[<p>No matter what programming language you use, there comes a time when you need to search for a file somewhere on the file system. Here, I want to talk about accomplishing this task in Perl. There are many ways of doing so, most of them boring, but I want to discuss the fun and elegant way &#8211; using <a href="http://search.cpan.org/dist/File-Find-Rule/lib/File/Find/Rule.pm" rel="nofollow">File::Find::Rule</a>.</p>
<p>Let me briefly discuss some of the other methods first.</p>
<h2>Limited</h2>
<p>Using glob() (or &#60;&#62;, TODO verify) you can find files in a single directory, using only the limited shell wildcard support. For example,</p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre>my @files =</pre></td></tr></table></div><p>...<div class=clear></div> <a href="http://beerpla.net/2009/04/08/perl-finding-files-the-fun-and-elegant-way/" class="read_more"><div class=excerpt-end>Read the rest of this article &#187;</div></a></p>]]></description>
			<content:encoded><![CDATA[<p>No matter what programming language you use, there comes a time when you need to search for a file somewhere on the file system. Here, I want to talk about accomplishing this task in Perl. There are many ways of doing so, most of them boring, but I want to discuss the fun and elegant way &#8211; using <a href="http://search.cpan.org/dist/File-Find-Rule/lib/File/Find/Rule.pm" rel="nofollow">File::Find::Rule</a>.</p>
<p>Let me briefly discuss some of the other methods first.</p>
<h2>Limited</h2>
<p>Using glob() (or &lt;&gt;, TODO verify) you can find files in a single directory, using only the limited shell wildcard support. For example,</p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre>my @files = glob(&quot;tmp*&quot;);</pre></td></tr></table></div>
<p><div class="note"><div class="noteclassic">I prefer glob() to &lt;&gt; because glob()&#039;s parameters can be more than just text (for ex functions) while &lt;&gt; treats everything inside as text.</div></div></p>
<h2>Boring</h2>
<p><a title="http://search.cpan.org/~nwclark/perl-5.8.9/lib/File/Find.pm" href="http://search.cpan.org/~nwclark/perl-5.8.9/lib/File/Find.pm">File::Find</a> is the de facto standard for searching in Perl.</p>
<p>This method finds files that end in .pl in &quot;.&quot; and &quot;../SomeDir&quot;, following symlinks:</p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre>#!/usr/bin/perl -w
&nbsp;
use File::Find;
use Data::Dumper;
use File::Basename;
my @directories_to_search = (&quot;.&quot;, &quot;../SomeDir&quot;);
my @file_list = ();
&nbsp;
find(
  { wanted =&gt;
    sub {
      if ( basename($File::Find::name) =~ /\.pl$/i )
      {
        push @file_list, $File::Find::name;
      }
    },
    follow =&gt; 1
  },
  @directories_to_search
);
print Dumper @file_list;</pre></td></tr></table></div>
<p>It works fine, except it&#039;s horribly ugly and boring. Let&#039;s have a look at something more fun.</p>
<h2>The Fun And Elegant Way</h2>
<p><a title="http://search.cpan.org/dist/File-Find-Rule/lib/File/Find/Rule.pm" href="http://search.cpan.org/dist/File-Find-Rule/lib/File/Find/Rule.pm" rel="nofollow">File::Find::Rule</a>. Just have a look at this beauty.</p>
<p>Just like above, find all .pl files in &quot;.&quot; and &quot;../SomeDir&quot;, following symlinks:</p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre>print Dumper (File::Find::Rule-&gt;name(&quot;*.pl&quot;)-&gt;file-&gt;extras({ follow =&gt; 1 })-&gt;
in(&quot;.&quot;, &quot;../SomeDir&quot;));</pre></td></tr></table></div>
<p>Same as above, except bypass .svn directories (shaves off a ton of time with a lot of directories):</p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre>print Dumper (File::Find::Rule-&gt;not(File::Find::Rule-&gt;directory-&gt;name('.svn')-&gt;
prune-&gt;discard)-&gt;name(&quot;*.pl&quot;)-&gt;file-&gt;extras({ follow =&gt; 1 })-&gt;in(&quot;.&quot;, &quot;../SomeDir&quot;));</pre></td></tr></table></div>
<p>Find all .log files that are older than 24 hours in &quot;.&quot;</p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre>my $epoch_time_1_day_ago = time() - 60*60*24;
print Dumper (File::Find::Rule-&gt;file-&gt;name(&quot;*.log&quot;)-&gt;
mtime(&quot;&lt;$epoch_time_1_day_ago&quot;)-&gt;in('.'));</pre></td></tr></table></div>
<p>Be sure to read the <a href="http://search.cpan.org/dist/File-Find-Rule/lib/File/Find/Rule.pm" rel="nofollow">File::Find::Rule</a> perldoc for more options and remember: have fun with your code!<a href="http://www.weblocal.ca"></a></p>
<p>Thanks to <a href="http://perlbuzz.com/" rel="nofollow">Perlbuzz</a> and Andy Lester for <a href="http://perlbuzz.com/mechanix/2008/05/optimizing-file-searches-with.html" rel="nofollow">pointing me</a> to this library a few months ago.</p>
<div class="shr-bookmarks shr-bookmarks-expand">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=%5BPerl%5D+Finding+Files%2C+The+Fun+And+Elegant+Way+-+http://bit.ly/caZusS&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://beerpla.net/2009/04/08/perl-finding-files-the-fun-and-elegant-way/&amp;t=%5BPerl%5D+Finding+Files%2C+The+Fun+And+Elegant+Way" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://beerpla.net/2009/04/08/perl-finding-files-the-fun-and-elegant-way/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-hackernews">
			<a href="http://news.ycombinator.com/submitlink?u=http://beerpla.net/2009/04/08/perl-finding-files-the-fun-and-elegant-way/&amp;t=%5BPerl%5D+Finding+Files%2C+The+Fun+And+Elegant+Way" rel="nofollow" class="external" title="Submit this to Hacker News">Submit this to Hacker News</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://beerpla.net/2009/04/08/perl-finding-files-the-fun-and-elegant-way/&amp;title=%5BPerl%5D+Finding+Files%2C+The+Fun+And+Elegant+Way" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://beerpla.net/2009/04/08/perl-finding-files-the-fun-and-elegant-way/&amp;title=%5BPerl%5D+Finding+Files%2C+The+Fun+And+Elegant+Way" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://beerpla.net/2009/04/08/perl-finding-files-the-fun-and-elegant-way/&amp;title=%5BPerl%5D+Finding+Files%2C+The+Fun+And+Elegant+Way" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://beerpla.net/2009/04/08/perl-finding-files-the-fun-and-elegant-way/&amp;title=%5BPerl%5D+Finding+Files%2C+The+Fun+And+Elegant+Way" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-mail">
			<a href="mailto:?subject=%22%5BPerl%5D%20Finding%20Files%2C%20The%20Fun%20And%20Elegant%20Way%22&amp;body=Link: http://beerpla.net/2009/04/08/perl-finding-files-the-fun-and-elegant-way/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A No%20matter%20what%20programming%20language%20you%20use%2C%20there%20comes%20a%20time%20when%20you%20need%20to%20search%20for%20a%20file%20somewhere%20on%20the%20file%20system.%20Here%2C%20I%20want%20to%20talk%20about%20accomplishing%20this%20task%20in%20Perl.%20There%20are%20many%20ways%20of%20doing%20so%2C%20most%20of%20them%20boring%2C%20but%20I%20want%20to%20discuss%20the%20fun%20and%20elegant%20way%20-%20using%20Fil" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
Similar Posts:<ul><li><a href="http://beerpla.net/2009/03/05/perl-how-to-get-the-path-of-an-included-library-pm-regardless-of-current-directory/" rel="bookmark" title="March 5, 2009">[Perl] How To Get The Path Of An Included Library (.pm), Regardless Of Current Directory</a></li>
<li><a href="http://beerpla.net/2007/10/28/ftprush-cleanup-script/" rel="bookmark" title="October 28, 2007">FTPRush Cleanup Script</a></li>
<li><a href="http://beerpla.net/2008/03/21/quick-snippet-finding-if-a-file-has-a-media-extension-using-regex/" rel="bookmark" title="March 21, 2008">Quick Perl Snippet: Finding If A File Has A Media Extension Using Regex</a></li>
<li><a href="http://beerpla.net/2008/07/23/how-to-check-if-the-local-svn-revision-is-up-to-date/" rel="bookmark" title="July 23, 2008">How To Check If The Local SVN Revision Is Up-To-Date</a></li>
<li><a href="http://beerpla.net/2008/10/11/how-to-sort-folders-the-same-way-as-files-in-total-commander/" rel="bookmark" title="October 11, 2008">How To Sort Folders The Same Way As Files In Total Commander</a></li>
</ul><!-- Similar Posts took 10.704 ms --><p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://beerpla.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://beerpla.net/2009/04/08/perl-finding-files-the-fun-and-elegant-way/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
