in reply to Re: Use Of Glob On File Extensions (find/rule)
in thread Use Of Glob On File Extensions

First, I would like to thank you for your help. Second, this is the first time I have heard of File::Find, so I had to study it. Is it possible to recursively go through directories with something like this?

#!/usr/local/bin/perl use strict; use warnings; use File::Find; my @argv; my $dir = $ARGV[0]; find(\&recursiveDir, $dir); sub recursiveDir{ return if -d; my @files = glob "${dir}/*pl"; foreach (@files) { next if -d; (my $txt = $_) =~ s/pl$/txt/; rename($_, $txt); } }

Replies are listed 'Best First'.
Re^3: Use Of Glob On File Extensions (find/rule)
by Anonymous Monk on Nov 19, 2014 at 01:06 UTC

    First, I would like to thank you for your help. Second, this is the first time I have heard of File::Find, so I had to study it. Is it possible to recursively go through directories with something like this?

    File::Find::Rule uses File::Find so that you don't have to -- use File::Find::Rule :) its easier

      Ok, I'll try that!

      Thank you!

Re^3: Use Of Glob On File Extensions (find/rule)
by Anonymous Monk on Nov 19, 2014 at 00:55 UTC
    Is it possible to recursively go through directories with something like this?
    You want glob to return files with extension .pl. So you can't recursively go through directories if glob doesn't return directories. Actually, it will return them, if they also end with .pl. But really, use opendir, readdir, closedir.

      File::Find::Rule uses opendir/readdir/closedir so that you don't have do -- work is for suckers

      This program tested

      #!/usr/bin/perl -- use strict; use warnings; use Path::Tiny qw/ cwd path /; use File::Find::Rule qw/ find rule /; path( "firstgonertoday/a.pl" )->touchpath; path( "firstgonertoday/me/too/b.pl" )->touchpath; rule( file => name => qr/\.pl$/i, exec => sub { print "@_\n"; return !!0; ## discard }, )->in( "firstgonertoday" ); path( "firstgonertoday" )->remove_tree; __END__ $ perl pathclassfilefindrulefirstgonertoday.pl a.pl firstgonertoday firstgonertoday/a.pl b.pl firstgonertoday/me/too firstgonertoday/me/too/b.pl
        File::Find::Rule uses opendir/readdir/closedir so that you don't have do -- work is for suckers
        I believe there is some value in learning how to write a recursive tree traversal function.

      Hmmm, thank you for your help Anonymous Monk. I think I might have bit off a bit more than I can chew right now with my level of Perl.

      I know this isn't right, but am I heading in the right direction?

      #!/usr/local/bin/perl use strict; use warnings; use File::Find; my @argv; my $dir = $ARGV[0]; find(\&recursiveDir, $dir); sub recursiveDir{ my @directories = glob "*"; if (-d) { opendir(DIR, $dir); while(readdir DIR) { my @files = glob "${dir}/*pl"; foreach (@files) { next if -d; (my $txt = $_) =~ s/pl$/txt/; rename($_, $txt); closedir (DIR); } } } }

      I am trying to grab all files and directories and store them into an array, then traverse through it like that.

        I'm not sure why you're so determined to use glob. No, you're not heading in the right direction, File::Find doesn't work like that at all.