<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Greetings,<div>I need to be able to process files within a directory tree.&nbsp;I know there is a module IO::Dir::Recursive, but I have trouble wrapping my brain around recursion, so I wanted to give it a try&nbsp;just using IO::Dir. This seems to do the recursion correctly:</div><div><br></div><div><div>#!/usr/bin/perl -w</div><div>use strict;</div><div>use IO::Dir;</div><div><br></div><div>my $source = $ARGV[0];</div><div>my $count;</div><div><br></div><div>process_dir($source);</div><div>print "$count files in all.\n";</div><div><br></div><div>sub check {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>my ($dh,$path) = @_;<span class="Apple-tab-span" style="white-space:pre">        </span>## dirhandle and path</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>while (defined($_ = $dh-&gt;read)) {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>next if m/^\.+/;<span class="Apple-tab-span" style="white-space:pre">                                </span>## skip dot files</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>if ( -f "$path/$_" ) {</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>my $file = "$path/$_";</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>process_file($file);</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>} elsif ( -d "$path/$_" ) {</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>my $dir = "$path/$_";</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>process_dir($dir);</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>} else { print "unknown thing\n"; }</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>undef $dh;<span class="Apple-tab-span" style="white-space:pre">        </span></div><div>}</div><div><br></div><div>sub process_file {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>my $file = shift;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>$count++;</div><div>}</div><div><br></div><div>sub process_dir {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>my $path = shift;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>my $dh = IO::Dir-&gt;new($path);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>check($dh,$path);</div><div>}</div><div><br></div><div>##&nbsp;... I'm guessing there's a better way, so I'm asking for feedback.&nbsp;</div><div><br></div><div>thanks,</div><div><br></div></div><div>
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; -webkit-text-decorations-in-effect: none; text-indent: 0px; -webkit-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; color: rgb(0, 0, 0); font-family: Verdana; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; -webkit-text-decorations-in-effect: none; text-indent: 0px; -webkit-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><span class="Apple-style-span" style="border-collapse: separate; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; -webkit-text-decorations-in-effect: none; text-indent: 0px; -webkit-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><div style="font-family: Helvetica; "><span class="Apple-style-span" style="font-family: Helvetica; ">Tom</span></div><div style="font-family: Helvetica; "><span class="Apple-style-span" style="font-family: Helvetica; "><a href="mailto:kellert@ohsu.edu">kellert@ohsu.edu</a></span></div><div style="font-family: Helvetica; "><span class="Apple-style-span" style="font-family: Helvetica; ">503-494-2442</span></div></span></span></div></span></div></span><br class="Apple-interchange-newline"></div></span><br class="Apple-interchange-newline"></div></span><br class="Apple-interchange-newline"></div></span><br class="Apple-interchange-newline"></div></span><br class="Apple-interchange-newline"></span><br class="Apple-interchange-newline">
</div>
<br></body></html>