[Pdx-pm] recursive IO::Dir

Erik Hollensbe erik at hollensbe.org
Thu Feb 18 12:41:44 PST 2010


Instead of plugging my own software, I'll mention File::Find because no 
one has yet. It should already be on your system.

On 02/18/10 14:04, Tom Keller wrote:
> Greetings,
> I need to be able to process files within a directory tree. 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 just using 
> IO::Dir. This seems to do the recursion correctly:
>
> #!/usr/bin/perl -w
> use strict;
> use IO::Dir;
>
> my $source = $ARGV[0];
> my $count;
>
> process_dir($source);
> print "$count files in all.\n";
>
> sub check {
> my ($dh,$path) = @_; ## dirhandle and path
> while (defined($_ = $dh->read)) {
> next if m/^\.+/; ## skip dot files
> if ( -f "$path/$_" ) {
> my $file = "$path/$_";
> process_file($file);
> } elsif ( -d "$path/$_" ) {
> my $dir = "$path/$_";
> process_dir($dir);
> } else { print "unknown thing\n"; }
> }
> undef $dh;
> }
>
> sub process_file {
> my $file = shift;
> $count++;
> }
>
> sub process_dir {
> my $path = shift;
> my $dh = IO::Dir->new($path);
> check($dh,$path);
> }
>
> ## ... I'm guessing there's a better way, so I'm asking for feedback.
>
> thanks,
>
> Tom
> kellert at ohsu.edu <mailto:kellert at ohsu.edu>
> 503-494-2442
>
>
>
>
>
>
>
>
> _______________________________________________
> Pdx-pm-list mailing list
> Pdx-pm-list at pm.org
> http://mail.pm.org/mailman/listinfo/pdx-pm-list

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/pdx-pm-list/attachments/20100218/02e64547/attachment.html>


More information about the Pdx-pm-list mailing list